Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

regex issue

by pinnacle (Acolyte)
on Nov 10, 2010 at 20:31 UTC ( #870685=perlquestion: print w/replies, xml ) Need Help??

pinnacle has asked for the wisdom of the Perl Monks concerning the following question:

Question

I am trying to print from line 2 to line 6

#!/usr/bin/perl use strict; while(my $inp = <DATA>){ if ($inp =~ /^Lesle/ && /43500$/){ print "$inp\n"; }} __DATA__ 41 Tommy Savage:4087240140: 12 2 2 Oxbow Court, Sunnyvale, CA 94087 +: 5/19/66: 34200 42 Lesle Kerstin: 408456123 4: 4 Harvard Square, Boston, MA 02133: +4/22/62: 52600 43 JonDeLoach: 4082533 122: 12 3 Park St. , San Jose, CA 94086: 7/2 +5/53: 85100 44 Ephram Hardy:2932595395: 2 3 5 Carlton Lane, Joliet, IL 73858: 8 +/12/20: 56700 45 etty Boop: 24583683 57: 63 5 Cutesy Lane, Hollywood, CA 91464: 6 +/23/23: 14500 46 Wilhelm Kopf:8468362837 : 693 7 Ware Road, Milton, PA 93756: 9/2 +1/46: 43500 47 Norma Corder:397857 2735: 74 Pine Street, Dearborn, MI 23874: 3/ +28/45: 245700 48 James Ikeda: 8349388376: 2 3 445 Aster Ave. , Allentown, NJ 8374 +5: 12/1/38: 45000 49 Lori Gortz: 3278325728: 3 465 Mirlo Street, Peabody, MA 34756: 1 +0/2/65: 35200 50 Barbara Kerz:385573 8326: 83 2 Ponce Drive, Gary, IN 83756: 12/1 +5/46: 268500

Please assist why I am not getting result, even tough syntax is ok. Thanks!!

Replies are listed 'Best First'.
Re: regex issue
by d5e5 (Beadle) on Nov 10, 2010 at 21:07 UTC
    ^ in a regex pattern means 'starts with'. No line starts with Lesle.

    /43500$/ is incomplete. You have to say match it with $inp.

    Try this:

    #!/usr/bin/perl use strict; use warnings; while(my $inp = <DATA>){ if ($inp =~ /Lesle/ .. $inp =~ /43500$/){ print "$inp\n"; } } __DATA__ 41 Tommy Savage:4087240140: 12 2 2 Oxbow Court, Sunnyvale, CA 94087 +: 5/19/66: 34200 42 Lesle Kerstin: 408456123 4: 4 Harvard Square, Boston, MA 02133: +4/22/62: 52600 43 JonDeLoach: 4082533 122: 12 3 Park St. , San Jose, CA 94086: 7/2 +5/53: 85100 44 Ephram Hardy:2932595395: 2 3 5 Carlton Lane, Joliet, IL 73858: 8 +/12/20: 56700 45 etty Boop: 24583683 57: 63 5 Cutesy Lane, Hollywood, CA 91464: 6 +/23/23: 14500 46 Wilhelm Kopf:8468362837 : 693 7 Ware Road, Milton, PA 93756: 9/2 +1/46: 43500 47 Norma Corder:397857 2735: 74 Pine Street, Dearborn, MI 23874: 3/ +28/45: 245700 48 James Ikeda: 8349388376: 2 3 445 Aster Ave. , Allentown, NJ 8374 +5: 12/1/38: 45000 49 Lori Gortz: 3278325728: 3 465 Mirlo Street, Peabody, MA 34756: 1 +0/2/65: 35200 50 Barbara Kerz:385573 8326: 83 2 Ponce Drive, Gary, IN 83756: 12/1 +5/46: 268500

Re: regex issue
by Roy Johnson (Monsignor) on Nov 10, 2010 at 20:44 UTC
    You want ".." instead of "&&".

    Caution: Contents may have been coded under pressure.
      ...also none of your data even begins with a letter.
Re: regex issue
by 7stud (Deacon) on Nov 10, 2010 at 20:39 UTC
    How would your write the condition: if x is greater than 10 and less than 20 print "hello"?
Re: regex issue
by aquarium (Curate) on Nov 10, 2010 at 23:48 UTC
    untested
    while(my $inp = <DATA>) { print "$inp\n" if($. >1 && $. <7); }
    the hardest line to type correctly is: stty erase ^H

      ...or more legibly

      use strict; use warnings; use IO::Handle; my $current_line_num; while (my $line = <DATA>) { $current_line_num = DATA->input_line_number(); if( $current_line_num >= 2 and $current_line_num <= 7) { print $line; } }

      And if the file is only 100 or so lines long, how about an array slice?

      for ( (<DATA>)[1..6] ) { print; }
        ...or more legibly

        I think you might be obscuring the wood with a lot of trees there! Perhaps use of the -n switch would be applicable here, either in a script or as a one-liner.

        $ cat rubbish Line1 Line2 Line3 Line4 Line5 Line6 Line7 Line8 Line9 Line10 Line11 Line12 $ perl -ne 'print if 2 .. 7;' rubbish Line2 Line3 Line4 Line5 Line6 Line7 $

        The array slice method is, as you say, only suitable for small files whereas using -n when combined with last is more efficient when interested only in lines at the beginning of large files. The following examples were acting on a 2,000,000+ line log file.

        # time perl -ne 'print if 5 .. 10' maillog Nov 11 04:19:28 ... Nov 11 04:19:28 ... Nov 11 04:19:28 ... Nov 11 04:19:28 ... Nov 11 04:19:28 ... real 0m1.206s user 0m0.773s sys 0m0.432s # time perl -ne 'print if 5 .. 10; last if $. > 10' maillog Nov 11 04:19:28 ... Nov 11 04:19:28 ... Nov 11 04:19:28 ... Nov 11 04:19:28 ... Nov 11 04:19:28 ... real 0m0.013s user 0m0.006s sys 0m0.008s #

        I hope this is of interest.

        Cheers,

        JohnGG

      It works!! thanks

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://870685]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (1)
As of 2023-05-31 01:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?