Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: How to create hash out of an array elements?

by ikegami (Pope)
on Nov 17, 2011 at 09:07 UTC ( #938551=note: print w/ replies, xml ) Need Help??


in reply to How to create hash out of an array elements?

The map callback never gets called since you didn't pass any list to transform, so map returns nothing.

You could create the desired list of two elements as follows:

my %sizes = ( $symbolsizes[0] => $symbolsizes[1] );

But really, all you need is the following:

my %sizes = @symbolsizes;

PS — The language is called "Perl", not "PERL".


Comment on Re: How to create hash out of an array elements?
Select or Download Code
Re^2: How to create hash out of an array elements?
by jojojo (Initiate) on Nov 17, 2011 at 09:19 UTC

    Thanks. This seems to be working. Now I am not able to print the values. The following code works:

    print "The size for $symbolsizes[0] is $sizes{$symbolsizes[0]}";
    But when I specify the name, it doesnt work. And this code is of no use if it can't print the values when the function name is specified. Please help.
    print "The size for function is $sizes{function}";

    Assume function to be an element of the array symbolsizes.

      Then $symbolsizes[0] doesn't contains function. Perhaps it contains a trailing newline that you didn't remove using chomp?

        And to see exactly what your hash does contain, use Data::Dumper (or one of its descendants). (I'm passing Dumper a reference to the hash because it prints it in a more hash-like manner that way.)

        abaugher@Aliantha ~$ cat doit #!/usr/bin/perl use Modern::Perl; my @array = ( 'key', 'value' ); my %hash = @array; say "The value of 'key' is '$hash{key}'"; use Data::Dumper; print Dumper \%hash; abaugher@Aliantha ~$ perl doit The value of 'key' is 'value' $VAR1 = { 'key' => 'value' };

        Aaron B.
        My Woefully Neglected Blog, where I occasionally mention Perl.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2014-10-20 04:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (72 votes), past polls