Maybe this is what you need?
#!/usr/bin/perl
use warnings;
use strict;
my @array;
while ( my $line = <DATA> ) {
chomp $line;
push (@array = split(/\s+/, $line,-1));
}
for my $item ( @array ) {
print $item,"\n";
}
Kindest Regards,
Bill Stephenson
On Jun 5, 2012, at 4:15 PM, Chris Stinemetz wrote:
> Why does this short program only seem to capture the last line of
> input in the @array, but when I put the for loop inside the while loop
> all lines of input are available in @array.
>
> I thought by declaring the @array outside the while loop would make
> all of its contents available once all the lines are read in the while
> loop.
>
> Thanks,
>
> Chris
>
> #!/usr/bin/perl
> use warnings;
> use strict;
>
> my @array;
>
> while ( my $line = <DATA> ) {
> chomp $line;
> @array = split(/\s+/, $line,-1);
> }
>
> for my $item ( @array ) {
> print $item,"\n";
> }
>
> __DATA__
> Line1 c 2 3 4 5 C 7 8 9
> Line2 1 2 3 4 5 6 7 8 9
> Line3 1 2 3 4 5 D 7 8 9
> Line4 1 2 3 4 5 aDC6 7 8 9
> Line5 1 2 3 4 5 D 7 8 9
> Line6 1 2 3 4 5 dcDC 7 8 9
>
> --
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
> http://learn.perl.org/
>
>
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/