Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2003
    Location
    san jose, CA
    Posts
    68

    Unanswered: loop through while(<>)?

    Hello Everyone:

    I am doing a exercise which require me to read a text file from command line and using regular expressions to match differend criterias.

    I wrtoe code for three criteiras. I tested each criteria sepriately(it works). But When I try to group them together inside while loop, the results are shown in random order (e.g. results from each criteria are mixed together).

    I want to loop through the text file and show results for each criteia in order. Here is my code, please give me some suggestions on
    how to show each criteria in a orderly sequence. I appreicate your help!

    I tired to use while(1) inside while(<>) loop; I also try to assign the text to an array, then use foreach loops for each criteria(it doesn't work either). These two methos both failed.
    Could someone give me some suggestions on how to loop through the text file and test each criterias?
    I attached my source code here. Please take a look. Thanks!

    use strict;
    use warnings;

    my $criteria_A = 1;
    my $criteria_B = 2;
    my $criteria_C = 3;

    while(<>)
    {


    #CRITERIA A:use regular expression to match words that contain all five vowels
    # use modifier 'i' to turn off the case sensitivity

    if (/a/i and /e/i and /i/i and /o/i and /u/i)
    {
    print "$criteria_A - $_ \n";
    }


    #CRITERIA B:use Greedy Metacharacters '*' and '?' to match words begin and end with an 'a'

    if(/^a.*?a\b/i) # /^a.*a\b/matched the words doesn't end with 'a' as well
    {
    sleep 1;
    print "$criteria_B - $_ \n";
    }


    #CRITERIA C: use regular expression to match words contain five or more i's.
    if(/.*i{5,}/i ) # I didn't catch any words that contains five
    #or more i's. Please tell me if this is correct!
    {
    #sleep 1;
    print "$criteria_C - $_ \n";
    }



    }

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    I suspect that the push function is what you are looking for, assuming that I understand what you want.

    -PatP

  3. #3
    Join Date
    Oct 2002
    Location
    Baghdad, Iraq
    Posts
    697
    I'd just read it into an array and use the grep function to run your tests. Then use join to put them together into a nice list.

    I'm not going to give you the code. Read perlfunc.

  4. #4
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Quote Originally Posted by sco08y
    I'd just read it into an array and use the grep function to run your tests. Then use join to put them together into a nice list.

    I'm not going to give you the code. Read perlfunc.
    That's probably the easiest way to do it, assuming you have enough memory to read the whole tamale then process it. I tend to take a more conservative approach, even if it isn't always truly Perl-ish.

    -PatP

  5. #5
    Join Date
    Jun 2004
    Location
    Nowhere Near You
    Posts
    89
    Will this do?

    Code:
    #! -w
    use strict;
    use warnings;
    
    my(@a_RE1,@a_RE2,@a_RE3);
    map {push(@a_RE3,$_) if (/(i.*){5,}/); $_}
      map {push(@a_RE2,$_) if (/^a.*a$/); $_}
        map {push(@a_RE1,$_) if (/a/i and /e/i and /i/i and /o/i and /u/i); $_} <DATA>;
    print "Expression 1\n".join '',@a_RE1;
    print "Expression 2\n".join '',@a_RE2;
    print "Expression 3\n".join '',@a_RE3;
    
    __DATA__
    awordwitheu
    awordendingwithana
    awordwithfiveiiis
    awordwithmorethanfiveiiiiiis
    awurdhaseverythinglotsofiiiisa
    Of course, this would be more conventional

    Code:
    ...
    my(@a_RE1,@a_RE2,@a_RE3);                                           
    while (<DATA>) {                                                    
      push(@a_RE3,$_) if (/(i.*){5,}/);                                 
      push(@a_RE2,$_) if (/^a.*a$/);                                    
      push(@a_RE1,$_) if (/a/i and /e/i and /i/i and /o/i and /u/i);    
       };                                                               
    print "Expression 1\n".join '',@a_RE1;                              
    print "Expression 2\n".join '',@a_RE2;                              
    print "Expression 3\n".join '',@a_RE3;                              
    ...
    Last edited by senza_nome; 01-03-05 at 01:30.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •