Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

array refernce as hash value

by Anonymous Monk
on Jul 22, 2009 at 10:09 UTC ( #782233=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

my %hash ={ key1 => [a1_ref,a1_ref], key2 => [a3_ref,a4_ref] } # where a1_ref,a2_ref,a3_ref,a4_ref are array refernece
is this possible in perl How can i puch the array refernce and retriveve the elements? thanks

Replies are listed 'Best First'.
Re: array reference as hash value
by davorg (Chancellor) on Jul 22, 2009 at 10:15 UTC

    It's possible, but it's not doing what you think it is (as use warnings will show you).

    $ perl -Mwarnings -e '%h = { foo => [ 1 .. 3] }' Reference found where even-sized list expected at -e line 1.

    You have the wrong kind of brackets. You want:

    my %hash = ( key1 => [a1_ref,a1_ref], key2 => [a3_ref,a4_ref] );

    Having created your data structure, Data::Dumper will be invaluable for exploring it and perldsc is full of information about how to get values out of it.

    --

    See the Copyright notice on my home node.

    Perl training courses

Re: array refernce as hash value
by Crian (Deacon) on Jul 22, 2009 at 10:16 UTC

    Do you think of something like this?

    my %hash = ( key1 => [\@a1_ref, \@a2_ref], key2 => [\@a3_ref, \@a4_ref], );

    If you want to use the a2_ref for instanze, $hash{key1}->[1] could be what you want.


    Edit: As an example:

    #!/usr/bin/perl use strict; use warnings; my @array1 = ( 1 .. 6 ); my @array2 = ( 7 .. 9 ); my @array3 = ( 'aa' .. 'af' ); my @array4 = ( 'b0' .. 'b9' ); my %hash = ( key1 => [\@array1, \@array2], key2 => [\@array3, \@array4], ); print "hoffentlich eine acht: ", $hash{key1}->[1]->[1], "\n";

    It prints "hoffentlich eine acht: 8".

      I am trying to do somthing like this <code> open my $fh, '<', $filename or die "$filename: $!\n"; while (<$fh>) { chomp; ($key, $value) = split /\|/, $_; push @{$hash_merge{$key}},$value; print "key for $key merge values $hash_merge{$key}->[0]1 \n"; #file looks like
        sorry some typo mistake I am trying to do somthing like this
        open my $fh, '<', $filename or die "$filename: $!\n"; while (<$fh>) { + chomp; ($key, $value) = split /\|/, $_; push @{$hash_merge{$key}},$v +alue; print "key for $key merge values $hash_merge{$key}->[0]1 \n"; #file looks like 9344220001|sei10720013,21/07/2009-00-00-00,23/07/2009-12-34-44 9344220001|sei10720014,23/07/2009-20-00-23
          A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2022-06-29 18:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (97 votes). Check out past polls.

    Notices?