morgon has asked for the wisdom of the Perl Monks concerning the following question:
this is a way to get the unique elements of an array:
I would like to do away with the temporary hash and would like to write the following seemingly natural perl-code:my @array = (1,1,2,3,2,2); my %temp = map { $_ => 1 } @array; my @unique = keys %temp;
But to my amazement this does not work with 5.18.1 - the error is "Type of argument to keys on reference must be unblessed hashref or arrayref".my @unique = keys (map { $_ => 1 } @array);
I could swear this used to work some years ago (not really sure), so my question is how can I do this - how can I use keys with such an expression?
Is there really no way to avoid the ugly temporary?
Please note that the above is just an illustration - this is not a question about how to find unique array elements but a question concerning the kinds of expressions I can feed into keys.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: keys with an expression
by hdb (Monsignor) on Nov 20, 2013 at 09:49 UTC | |
|
Re: keys with an expression (List::AllUtils::uniq
by Anonymous Monk on Nov 20, 2013 at 10:08 UTC | |
|
Re: keys with an expression
by tobyink (Canon) on Nov 20, 2013 at 12:57 UTC | |
|
Re: keys with an expression
by lune (Pilgrim) on Nov 20, 2013 at 15:04 UTC | |
|
Re: keys with an expression
by AnomalousMonk (Archbishop) on Nov 20, 2013 at 19:16 UTC |