grinder has asked for the wisdom of the Perl Monks concerning the following question:
I have the following routine:
sub remap { my %remap = qw{ THIS THAT BLACK WHITE TALL SHORT }; $remap{ $_[0] }; }
Put in TALL, get out SHORT... The use of a lexical sort of strikes me as being a little unnecessary, what I would like to do would be to just build an anonymous hash out of the qw list, and dereference it:
sub remap_anon { @{[qw{ THIS THAT BLACK WHITE TALL SHORT }]}->{$_[0]}; }
Cool! That actually compiles. I was tickled pink. Trouble is, I don't know what it does. It certainly doesn't do what I want it to. If I try to run it I get: Can't coerce array into hash at remapper line 15
So I googled around a bit, and the only solutions I see are what I had in the first place: dump the list into a hash and then dereference it. Drat. I tried enclosing the anonymous @{[...]} in an outer %{...] or even %{[...]}, but perl continues to choke with the same message. At this stage it's just shotgun debugging, and besides, the syntax is a net loss.
So my question is, even if perl can't do it by itself (because "You used an array where a hash was expected, but the array has no information on how to map from keys to array indices. You can do that only with arrays that have a hash reference at index 0"), is there a way to coerce perl to coerce an array into a list? And what does that message really mean?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Coercing an array into a hash
by broquaint (Abbot) on Dec 05, 2002 at 12:10 UTC | |
|
Re: Coercing an array into a hash
by Zaxo (Archbishop) on Dec 05, 2002 at 12:41 UTC | |
|
Re: Coercing an array into a hash
by PodMaster (Abbot) on Dec 05, 2002 at 12:17 UTC |