[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] pop (keys %hash) ?



At 12.55 -0400 1999.06.18, jean-Francois Jobidon wrote:
>@listKeys = (keys %hash);
>$lastkey = pop @listKeys;
>
>I'd like to know if there is a way to write this code
>on one line. Something like :
>
>$lastkey = pop (keys %hash);
>
>Perl tells me that the argument must be an array.

Right.  pop() is about order, and hashes have no order.

You could do something like:

  $element = pop @{[keys %hash]};

This puts keys %hash into an anonymous array reference [], and then @{}
dereferences it to be used as an array.

I wouldn't call it "lastkey", because there is no such thing as the last
key of a hash, as they are unordered, and the order may change from one
time to the next.

--
Chris Nandor          mailto:pudge@pobox.com         http://pudge.net/
%PGPKey = ('B76E72AD', [1024, '0824090B CE73CA10  1FF77F13 8180B6B6'])

===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org