Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: array refernce as hash value

by Crian (Chaplain)
on Jul 22, 2009 at 10:16 UTC ( #782239=note: print w/replies, xml ) Need Help??


in reply to array refernce as hash value

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".

Replies are listed 'Best First'.
Re^2: array refernce as hash value
by Anonymous Monk on Jul 22, 2009 at 10:28 UTC
    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
        Are you looking for something similar to :
        use strict; use Data::Dumper; my %hash; while (<DATA>) { chomp; my ($key, $value) = split /\|/, $_; my @values = split /,/, $value; if (defined $hash{$key}) { push @{$hash{$key}}, \@values; } else { $hash{$key} = [\@values]; } } print Dumper \%hash; __DATA__ 9344220001|sei10720013,21/07/2009-00-00-00,23/07/2009-12-34-44 9344220001|sei10720014,23/07/2009-20-00-23 9344220002|sei10720015,21/07/2009-00-00-00,23/07/2009-12-34-44 9344220003|sei10720016,23/07/2009-20-00-23 9344220003|sei10720017,23/07/2009-20-00-23 9344220003|sei10720018,23/07/2009-20-00-23
        Output is as followed:
        $VAR1 = { '9344220003' => [ [ 'sei10720016', '23/07/2009-20-00-23' ], [ 'sei10720017', '23/07/2009-20-00-23' ], [ 'sei10720018', '23/07/2009-20-00-23' ] ], '9344220002' => [ [ 'sei10720015', '21/07/2009-00-00-00', '23/07/2009-12-34-44' ] ], '9344220001' => [ [ 'sei10720013', '21/07/2009-00-00-00', '23/07/2009-12-34-44' ], [ 'sei10720014', '23/07/2009-20-00-23' ] ] };
        I hope this helps.
        Regards,
        Ashish

        And what is youre problem with this?

        #!/usr/bin/perl use strict; use warnings; my %hash_merge; while (<DATA>) { chomp; my ($key, $value) = split /\|/, $_; push @{$hash_merge{$key}}, $value; print "key for $key merge values: ", $hash_merge{$key}->[0], "\n"; } use Data::Dumper; print Dumper \%hash_merge; __DATA__ 9344220001|sei10720013,21/07/2009-00-00-00,23/07/2009-12-34-44 9344220001|sei10720014,23/07/2009-20-00-23
        prints
        key for 9344220001 merge values: sei10720013,21/07/2009-00-00-00,23/07 +/2009-12-34-44 key for 9344220001 merge values: sei10720013,21/07/2009-00-00-00,23/07 +/2009-12-34-44 $VAR1 = { '9344220001' => [ 'sei10720013,21/07/2009-00-00-00,23/07/200 +9-12-34-44', 'sei10720014,23/07/2009-20-00-23' ] };

        Is this, what you want, or is it not, what you want? In the latter case, what exactly do you want to have as result?

        in that case how will i get the first element in the first array here how can i get sei10720013??

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://782239]
help
Chatterbox?
[ambrus]: Corion: ah, so you want a library that parses HTTP, and you want to do the IO yourself, and don't want a full AnyEvent wrapper.
[ambrus]: Corion: I think I parsed a HTTP header from a string with LWP once. You can definitely use that to create a HTTP message too. The problme is
[ambrus]: that if you do that, you'd have to find where each HTTP response ends, which is nontrivial if you want persistent connections (essential for performance if you have small requests).

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (11)
As of 2016-12-07 15:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (130 votes). Check out past polls.