Perl Question: pop sort keys %hash

Ed Eddington ed at pcr7.pcr.com
Wed Oct 8 10:20:41 CDT 2003


A quick Perl oddity... I tried to grab the last element of a sorted list of hash keys in a quick one liner, but was forced to use an array variable. Anybody know why I can't do this? Sort *does* return an array, but pop complains at compile time that its argument isn't an array variable...

    "Type of arg 1 to pop must be array (not sort)"

I tried messing with parens and other bracketry to no avail. Just curious if anyone can explain this.

Ed

---------------
#!/bin/perl

my %hash;
$hash{abc}='1';
$hash{xyz}='2';

#my $lastkey = pop sort keys %hash;    # this doesn't work.

my @sorted = sort keys %hash;            # this works using an extra step
my $lastkey = pop @sorted;

print "last hash key is $lastkey\n";




More information about the grand-rapids-pm-list mailing list