Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Trouble accessing Hash of Arrays

by sujeet97 (Initiate)
on Apr 02, 2013 at 02:26 UTC ( #1026567=perlquestion: print w/ replies, xml ) Need Help??
sujeet97 has asked for the wisdom of the Perl Monks concerning the following question:

I am facing some trouble accessing values from a hash of arrays. I am attaching a sample code (this is not part of the main code but it pretty much shows the problem)

#!/usr/bin/perl %sampleHash = (); @array1 = ('a','b','c'); @array2=("x","y","z","s"); $sampleHash{"hash1"} = @array1; $sampleHash{"hash2"} = @array2; print $sampleHash{"hash1"}[0];

In the above code, the print statement is blank. I want it to show the value - a. Please help. Its urgent.

Comment on Trouble accessing Hash of Arrays
Download Code
Re: Trouble accessing Hash of Arrays (troubleshooting basics)
by Anonymous Monk on Apr 02, 2013 at 02:36 UTC

    Basic debugging checklist says  use strict; use warnings; especially if you want to cut your development time in half!

    It will point you at problems with your code, like this

    #!/usr/bin/perl -- use diagnostics; use strict; use warnings; use Data::Dump; my %sampleHash = (); my @array1 = ('a','b','c'); my @array2 = ("x","y","z","s"); $sampleHash{"hash1"} = @array1; $sampleHash{"hash2"} = @array2; dd \%sampleHash; print $sampleHash{"hash1"}[0]; __END__ $ perl fudge { hash1 => 3, hash2 => 4 } Can't use string ("3") as an ARRAY ref while "strict refs" in use at f +udge line 14 (#1) (F) Only hard references are allowed by "strict refs". Symbolic references are disallowed. See perlref. Uncaught exception from user code: Can't use string ("3") as an ARRAY ref while "strict refs" in +use at fudge line 14.

    @array in scalar context returns its size , perlintro, Tutorials: Context tutorial

    To store an array in a scalar, to store an array in a hash, you have to use the reference operator, reference-of operator, see perlreftut#Making References , references quick reference

Re: Trouble accessing Hash of Arrays
by Athanasius (Monsignor) on Apr 02, 2013 at 02:42 UTC

    Hello sujeet97, and welcome to the Monastery!

    As Anonymous Monk says, you need to store references to the two arrays. This:

    #! perl use strict; use warnings; my %sampleHash = (); my @array1 = ('a', 'b', 'c'); my @array2 = ('x', 'y', 'z', 's'); $sampleHash{hash1} = \@array1; $sampleHash{hash2} = \@array2; print $sampleHash{hash1}[0];

    prints a as desired. See perlreftut and perlref.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      yes. Got it. Thanks a lot :-)

Re: Trouble accessing Hash of Arrays
by vinoth.ree (Parson) on Apr 02, 2013 at 04:17 UTC
Re: Trouble accessing Hash of Arrays
by AnomalousMonk (Monsignor) on Apr 02, 2013 at 22:51 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2014-08-23 08:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (172 votes), past polls