http://www.perlmonks.org?node_id=938557


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

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.

Replies are listed 'Best First'.
Re^3: How to create hash out of an array elements?
by ikegami (Patriarch) on Nov 17, 2011 at 10:02 UTC

    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.