oko1 has asked for the wisdom of the Perl Monks concerning the following question:
My brain does not seem to be working well today; seems like I should be able to do this, but I can't come up with an answer - or even tell if it's possible. I'd appreciate it if the brilliant Monks here could stand in for some of my cerebral capacity... :)
Situation: take a typical hash slice; make an href out of it.
my @data = qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/; my %h; @h{1..12} = @data; my $x = \%h;
So far, so good. My question is, given the two lists above, how would you create $x without those other temporary variables? I've been fiddling with things like
# This gets me a hash... my %x = map { $_ + 1 => (qw/Jan Feb .../)[$_] } 0 .. 11; # ...but this does not get me a hashref :-\ my $x = \%{ map { $_ + 1 => (qw/Jan Feb .../)[$_] } 0 .. 11 };
and their ilk for a while, and am not getting it. Help would be appreciated!
[ Oh - and - no, this isn't some critical necessity; it's just me, always trying to learn more about Perl idiom. ]
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: On-the-fly hashref
by tirwhan (Abbot) on Sep 28, 2010 at 16:47 UTC | |
|
Re: On-the-fly hashref
by moritz (Cardinal) on Sep 28, 2010 at 16:52 UTC | |
|
Re: On-the-fly hashref
by hbm (Hermit) on Sep 28, 2010 at 16:49 UTC | |
|
Re: On-the-fly hashref
by Anonymous Monk on Sep 28, 2010 at 16:50 UTC | |
|
Re: On-the-fly hashref
by jwkrahn (Abbot) on Sep 28, 2010 at 20:08 UTC | |
|
Re: On-the-fly hashref
by BrowserUk (Patriarch) on Sep 28, 2010 at 21:07 UTC | |
|
Re: On-the-fly hashref
by eric256 (Parson) on Sep 28, 2010 at 20:42 UTC | |
|
Re: On-the-fly hashref
by oko1 (Deacon) on Sep 28, 2010 at 17:10 UTC | |
by muba (Priest) on Sep 29, 2010 at 01:18 UTC | |
|
Re: On-the-fly hashref
by ikegami (Patriarch) on Sep 29, 2010 at 05:25 UTC |