Using the GREP Function in PERL 5.8

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jonniethecodeprince
    New Member
    • Mar 2007
    • 35

    Using the GREP Function in PERL 5.8

    I've found a program and interpreter for PERL 5.8 so to everyone who tried to help me with that thank you.

    Now my attention turns to the GREP function in PERL. From what I understand GREP is a simple pattern matching function that searches for either the full element in a string or a first letter of an element, but if anyone can clarify this that would be great :)

    [CODE=perl]
    @lastNames = ('Sutherland',' Summers','Sunle y','Duran','Gri eve')
    @JustSNames = grep(/^S/, @lastNames);
    [/CODE]

    For example in the above code I have a list and a regular expression that is trying to search for all the elements in this list that start with a letter S and return the full element.
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Ok, so what is the problem? I ran what you have there and it works fine. If it is the fact that it does not print out the names, then it is because you haven't told it to do so. All you did was put them into an array. If you want them to print, try something like this:

    [code=perl]
    my @lastNames = ('Sutherland',' Summers','Sunle y','Duran','Gri eve')
    my @JustSNames = grep(/^S/, @lastNames);

    foreach my $line (@JustSNames)
    {
    print("$line \n");
    }
    [/code]

    That prints out the elements in the array @JustSNames.

    Regards,

    Jeff

    Comment

    • jonniethecodeprince
      New Member
      • Mar 2007
      • 35

      #3
      Hello,

      Well I compared our 2 bits of code and I believe I found where the problem was
      [code=perl]
      @lastNames = ('Sutherland',' Summers','Sunle y','Duran','Gri eve')[/code]

      In the above line there is a semi colon missing.

      [code=perl]
      @lastNames = ('Sutherland',' Summers','Sunle y','Duran','Gri eve');[/code]

      By simply adding the semi colon to the line, both code blocks seemed to work. The code on GREP that I worked from was very sketchy.

      Thanks. :D

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        You are completely correct. In fact, if this were in a script with warnings were used, you would have been given an error when executing. When I did this in a perl one liner, I had put in the semicolon after that line and didn't catch it. So, when I pasted the foreach statement in my posting, I had used your two lines. Must have wake-up juice before posting, I swear.

        Regards,

        Jeff

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          From what I understand GREP is a simple pattern matching function that searches for either the full element in a string or a first letter of an element, but if anyone can clarify this that would be great :)
          That is not quite correct. grep() uses an argument to find things in lists, the argument can be a regular expression but it can be other things as well. For example:

          @files = grep { (-d) } readdir DIR;

          same as:

          @files = grep( -d $_, readdir DIR);

          Comment

          Working...