On Tue, 4 Jul 2000, George Vieira wrote:
> hey all,
>
> How do you read lines from a file into an array using Perl ( I still have
> this Perl problem)
> in Basic it would be:
>
> open "file.txt" for input as #1
> while (eof(1))
> count=count+1
> input #1, line$(count)
> loop
> close #1
>
> what's the Perl version of this? I can't seem to get it to read one line
> each into an array.. it always grabs the whole file...
open FH, "<file.txt" or die "Cannot open: $!";
while (<FH>) {
++$count;
}
close FH;
Of course, you don't need to keep your own count, Perl has $.
<handle> is context senstive, in list context it reads everything, in
scalar context it reads a single "line"[1].
So you could do:
$array[0] = <FH>;
though this won't clear the rest of the array.
[1] <FH> will normally read a new-line separated line from FH, but you can
change this by fiddling with the $/ variable - see man perlvar
--
SLUG - Sydney Linux Users Group Mailing List - http://www.slug.org.au
To unsubscribe send email to [EMAIL PROTECTED] with
unsubscribe in the text