Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: regex issue

by aquarium (Curate)
on Nov 10, 2010 at 23:48 UTC ( [id://870727]=note: print w/replies, xml ) Need Help??


in reply to regex issue

untested
while(my $inp = <DATA>) { print "$inp\n" if($. >1 && $. <7); }
the hardest line to type correctly is: stty erase ^H

Replies are listed 'Best First'.
Re^2: regex issue
by 7stud (Deacon) on Nov 11, 2010 at 00:16 UTC

    ...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

Re^2: regex issue
by pinnacle (Acolyte) on Nov 10, 2010 at 23:51 UTC
    It works!! thanks

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://870727]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2024-04-23 13:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found