dwalin has asked for the wisdom of the Perl Monks concerning the following question:
Let's say I have a list of @objects interspersed with scalar values, and I want to get a list of unique stringified values of all @objects. Each $object has "" operator overloaded so naturally I try to do the intuitive thing:
my @objects = (); my %unique = map { "$_" => 1 } @objects; print sort keys %unique;
Contrary to my expectation, the construct above produces compiler error:
Not enough arguments for map at test.pl line 2, near "} @objects" syntax error at test.pl line 2, near "} @objects" Execution of test.pl aborted due to compilation errors.
Changing the offending line to...
my %unique = map { ''.$_ => 1 } @objects;
...resolves the error. But still there's something I can't understand about it. Why the more natural-looking construct is erroneous, while less intuitive works as expected? Is that another compiler quirk I just have to live with?
Regards,
Alex.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Strange compiler behavior with map?
by BrowserUk (Patriarch) on Sep 05, 2011 at 08:03 UTC | |
|
Re: Strange compiler behavior with map?
by ikegami (Patriarch) on Sep 05, 2011 at 08:05 UTC | |
by dwalin (Monk) on Sep 05, 2011 at 09:26 UTC | |
by Anonymous Monk on Sep 05, 2011 at 09:37 UTC | |
by ikegami (Patriarch) on Sep 06, 2011 at 00:51 UTC | |
|
Re: Strange compiler behavior with map?
by repellent (Priest) on Sep 05, 2011 at 08:05 UTC | |
by Anonymous Monk on Sep 05, 2011 at 08:28 UTC |