Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Declare and slice-initialize hash in one statement?

by davido (Cardinal)
on Oct 27, 2005 at 16:14 UTC ( [id://503395]=note: print w/replies, xml ) Need Help??


in reply to Declare and slice-initialize hash in one statement?

Your map solution is as close as you'll come, but you're right, if all you want to do is initialize a slice it's too longwinded.

You cannot declare a hash, and initialize a slice at the same time. Just do it this way:

my %hash; @hash{ list of keys } = ( list of values );

...where list of keys and list of values are obviously left up to you to fill in the blanks. BTW: Don't do the "no strict" method. Here's why. That version results in %hash being created as a package global, instead of as a lexical, and there is a big difference.


Dave

Replies are listed 'Best First'.
Re^2: Declare and slice-initialize hash in one statement?
by Perl Mouse (Chaplain) on Oct 27, 2005 at 16:31 UTC
    Note that the creation of the package hash isn't due to the absense of 'use strict', but the absense of a 'my' keyword.
    use strict; @::hash{@keys} = @values;
    creates a package hash as well.
    Perl --((8:>*

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://503395]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2024-04-23 11:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found