logo       

Re: Reading a list of numbers into an array: msg#00327

perl-beginners

Subject: Re: Reading a list of numbers into an array

On Wed, Jul 22, 2009 at 17:47, Emanuele Osimo<e.osimo@xxxxxxxxx> 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-unsubscribe@xxxxxxxx
For additional commands, e-mail: beginners-help@xxxxxxxx
http://learn.perl.org/


Google Custom Search

News | Mail Home | sitemap | FAQ | advertise