Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

On-the-fly hashref

by oko1 (Deacon)
on Sep 28, 2010 at 16:39 UTC ( #862464=perlquestion: print w/replies, xml ) Need Help??

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. ]


--
"Language shapes the way we think, and determines what we can think about."
-- B. L. Whorf

Replies are listed 'Best First'.
Re: On-the-fly hashref
by tirwhan (Abbot) on Sep 28, 2010 at 16:47 UTC

    my $x; @{$x}{1..12}=qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/;

    All dogma is stupid.
Re: On-the-fly hashref
by moritz (Cardinal) on Sep 28, 2010 at 16:52 UTC
    my $x = { map {; $_ + 1 => (qw/Jan Feb Mar/)[$_] } 0 .. 2 }

    Perl 6 solution:

    my $x = hash <Jan Feb Mar ...> Z 1..12;

    You can emulate that in Perl 5 with the zip() function from List::MoreUtils

    Perl 6 - links to (nearly) everything that is Perl 6.
Re: On-the-fly hashref
by hbm (Hermit) on Sep 28, 2010 at 16:49 UTC

    Is this what you want? (Note the curly braces.)

    my $x = { map { $_ + 1 => (qw/Jan Feb Mar Apr May/)[$_] } 0..4 }; print map { "$_ => $x->{$_}\n" } keys %$x;
Re: On-the-fly hashref
by Anonymous Monk on Sep 28, 2010 at 16:50 UTC
Re: On-the-fly hashref
by jwkrahn (Monsignor) on Sep 28, 2010 at 20:08 UTC
    my $x = { qw/ 1 Jan 2 Feb 3 Mar 4 Apr 5 May 6 Jun 7 Jul 8 Aug 9 Sep 10 + Oct 11 Nov 12 Dec / };
Re: On-the-fly hashref
by BrowserUk (Pope) on Sep 28, 2010 at 21:07 UTC
    My question is, given the two lists above, how would you create $x without those other temporary variables?

    Taking you literally that the values (@data array already exists, just autovivify the hashref:

    my $r; @{ $r }{ 1 .. 12 } = @data;

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: On-the-fly hashref
by eric256 (Parson) on Sep 28, 2010 at 20:42 UTC

    Heres how i would have done it...

    my $m = 1; my $hash = { map { $m++ => $_ } qw/Jan Feb Mar Apr May Jun Jul Aug Sep + Oct Nov Dec/ };

    ___________
    Eric Hodges
Re: On-the-fly hashref
by oko1 (Deacon) on Sep 28, 2010 at 17:10 UTC

    Thank you all so much, and ++ to everyone who has responded; I actually managed to remember the hashref constructor (and wrap the 'map' statement in it) while I was waiting for the answers. That's a hour or so that I wasted because my brain refused to give that bit of info up at the right time... I swear, I'd even talked to the bear before posting here, and even that didn't shake it loose. Five minutes of having the question up here in SoPW, and down it came. :)

    Again, my delighted thanks to everyone - and I'm truly grateful for _all_ the wonderful help you folks provide. An educated sounding board can be the absolutely most helpful thing possible, in the right circumstances!


    --
    "Language shapes the way we think, and determines what we can think about."
    -- B. L. Whorf

      Edit: Never mind, you already did try to translate the problem into your native language (didn't catch the "talk to the bear" part until after I read the article to which you linked, which was after I wrote my reply), so my reply no longer applies... Well, whatever.

Re: On-the-fly hashref
by ikegami (Pope) on Sep 29, 2010 at 05:25 UTC

    Why are you using a hash?

    my @months = qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/; ... $months[$month-1] ...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://862464]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2019-12-14 00:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?