Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

printing up until a certain line within a file

by c (Hermit)
on Oct 25, 2001 at 02:14 UTC ( [id://121290]=perlquestion: print w/replies, xml ) Need Help??

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

i'm working on printing the content of a file, but only up until a certain point. in my case, its until a line beginning with ## START shows up. my code

#!/usr/bin/perl -wT use strict; open(FH, "/testfile"); my @lines = <FH>; &test; close(FH); sub test{ for my $i(@lines) { if ($i =~ /^\#\# START/) { return; } else { print $i; } } }

this works, but it uses return and needs to be held within a subroutine to work. it seems like there should be a better way to do this, and i just am not crafty enough to come up with it. can someone point out another method that doesnt need nesting within a sub?

humbly -c

Replies are listed 'Best First'.
Or from the command line line . . .
by Fletch (Bishop) on Oct 25, 2001 at 03:30 UTC
Re: printing up until a certain line within a file
by dvergin (Monsignor) on Oct 25, 2001 at 02:43 UTC
    ...another method that doesnt need nesting within a sub

    How about:

    #!/usr/bin/perl -w use strict; open(FH, "/testfile"); while (my $line = <FH>) { last if $line =~ /^## START/; print $line; } close(FH);
Re: printing up until a certain line within a file
by lestrrat (Deacon) on Oct 25, 2001 at 02:31 UTC

    Well, how about this:

    sub print_up_to { my( $handle, $string ) = @_; while( <$handle> ) { last if /$string/o; print; } } use FileHandle; my $fh = FileHandle->new( 'foo' ); print_up_to( $fh, '^## START' ); ## of course, you should be able to do this: open( FOO, 'foo' ); print_up_to( \*FOO, '^## START' );
Re: printing up until a certain line within a file
by blackmateria (Chaplain) on Oct 25, 2001 at 02:52 UTC
    I think you can do this with the .. operator but I'm not sure if this is the best way. Maybe this will help, though:
    #!/usr/bin/perl -w use strict ; if (open (my $file, '/testfile')) { while (<$file>) { print if 1../^## START/ ; } close $file ; }
    Update: Oops, forgot to mention, the .. operator is documented in perlop. Thanks c.
Re: printing up until a certain line within a file
by Rex(Wrecks) (Curate) on Oct 25, 2001 at 04:43 UTC
    ++ for a nice clear question with the right amount of code.

    And a ++ for the oneliner solution..neat and to the point :)

    "Nothing is sure but death and taxes" I say combine the two and its death to all taxes!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-24 15:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found