Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Reading multiple lines?

by matthew (Acolyte)
on Nov 29, 2000 at 01:33 UTC ( [id://43778]=note: print w/replies, xml ) Need Help??


in reply to Reading multiple lines?

It's not terribly pretty, but it's cheap:
open FILE, "../data/foo.txt"; @lines = ($var1,$var2,$var3,$var4,$var5, $var6,$var7,$var8,$var9,$var10) = <FILE>; close FILE; print join "\n", @lines;
-Matthew

Replies are listed 'Best First'.
Re: Re: Reading multiple lines?
by matthew (Acolyte) on Nov 29, 2000 at 01:53 UTC
    Got an even better one:
    #!/usr/bin/perl my $file = "../data/foo.txt"; my @lines = getFileSlice($file,"10","1"); print join "", @lines; print "\n"; sub getFileSlice() { my $file = shift; my $length = shift; my $offset = shift; my @lines; open FILE, "../data/foo.txt"; for($i=0;$i<$offset;$i++) { <FILE> } for($i=0;$i<$length;$i++) { my $line = <FILE>; push @lines, $line; } close FILE; return @lines; }
    -Matthew
Re: Re: Reading multiple lines?
by Anonymous Monk on Nov 29, 2000 at 08:52 UTC
    This is even more simple:
    open FILE, "../data/foo.txt"; @lines = (<FILE>)[0..9]; close FILE; print "@lines";
Re: Re: Reading multiple lines?
by rdw (Curate) on Nov 29, 2000 at 14:11 UTC

    Well that's the kind of thing which would be nice to write, except I'd probably do it like this :-

    @lines[0..9] = <FILE>

    ...but it still reads the whole file in (it just throws most of it away). Firstly, that'll use too much memory in this case, and secondly you can't then read the next N lines in.

    Have fun,

    rdw

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2024-04-23 08:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found