On Wed, Jul 22, 2009 at 17:47, Emanuele Osimo<e.os...@gmail.com> wrote:
> Hello there, I'd like to read a file thet is a list of numbers like:
> 3084
> 4855
> 2904
> making each line (each number) one of the elements of the array. I've tried
> foreach my $line (<FILEHANDLE>){ push (@array, $line) }
> but then printing the array just gives me the last number, in this case
> "2904".
> What's wrong?
>
> Thanks
> Emanuele
>

Most likely the file has carriage returns in it and you are on Linux
(or another UNIX-like OS).  Try this:

while (my $line (<FILEHANDLE>) {
    $line =~ s/[\r\n]//g;
    push @array, $line;
}
print "numbers in @array:", join(", ", @array), "\n";


-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to