Chris Allen said:
I'm really stuck on the syntax for this one.
I want to do the equivalent of:
%a=('a'=>1,'b'=>2,'c'=>3);
@temp=keys %a;
$b=\@temp;
$b = [ keys %a ];
The above creates an anonymous array reference, and populates the
referenced array with the list of values returned by the keys function
But I can't find a single statement that will
do it. I've tried:
$b= \(keys %a);
Here you're generating a list of values, in a (seemingly) random order.
On my machine, that list came out to ('c', 'a', 'b'). The \ operator is
actually being applied to all three elements, effectively creating this
statement:
$b = (\'c', \'a', \'b');
The comma operator used in scalar context returns the last element
evaluated, so you're left with $b being set equal to a reference to the
string 'b'.
This should work, but is a bit ugly. You're telling Perl $b is an array
reference, and then assigning the list of values returned by keys to the
array referenced by $b. How did this "not work" for you?
This is trying to evaluate whatever is returned by the keys function as
an array reference, dereference it, and then take reference to the
result. I'm not surprised that didn't work. I am surprised it didn't
cause a fatal error, however. I suspect Perl is trying to do something
similar to the first case above, but I'm not positive.
They don't do the above - and actually I don't understand
why they do what they do...
Hope this helps,
Paul Lalli