[LA.pm] push question
Brian Cooke
mrkoffee at saltedsnail.com
Thu Mar 3 22:46:09 PST 2005
>
> I'm reading an array from a file:
>
> @lines = <FILE>;
>
...
>
> @lines={1,2,3};
> push(@lines,4);
> push(@lines,5);
>
> foreach(@lines)
> { printf "%s-", $_
> }
>
> Here's the output:
>
> % perl pushtest.pl
> HASH(0x334ca0)-4-5
>
> What I want is 1-2-3-4-5. How?
>
The {1,2,3} assignment places an anonymous hash reference in the first
element of @lines. Warnings enabled or -w would have alerted you about
an odd number of elements in that anonymous hash. So the first element
is a reference to a hash, and the second and third are 4 and 5.
This will give you the 1-2-3-4-5 you're looking for:
#!/usr/bin/perl
use warnings;
use strict;
my @lines = (1,2,3);
push @lines, 4;
push @lines, 5;
print join('-', @lines), "\n";
Not sure where the file slurp comes in, though, in relation to the
later assignment to (1,2,3)...
Cheers,
Brian
More information about the Losangeles-pm
mailing list