Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Making a hash of arrays from a while loop

by why_bird (Pilgrim)
on Mar 04, 2008 at 09:27 UTC ( [id://671828]=perlquestion: print w/replies, xml ) Need Help??

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

Good morning monks,

What I want to do is create a hash of arrays. The problem is that I want to do it by iterating through a while loop. The loop calculates the value of a variable $variable and an array @array. Each time the loop executes, I want to add the $variable as a key in my hash pointing to @array. I think it's the case that you have to pass arrays to hashes as references, so I'm having difficulty storing the value of the array (actually I'm also having trouble storing separate values for the key too, and I don't know why) rather than just getting the last key-value pair from the loop as my whole hash each time I run the program. Does that make sense? Any ideas? I was thinking about making an anonymous array from the values in @array, but don't know how to do that for an array that already exists.. I know this isn't a very well forumlated question, but please help if you can! (p.s. I don't know how many times the loop is going to execute)
Thanks :)

........
Those are my principles. If you don't like them I have others.
-- Groucho Marx
.......

Replies are listed 'Best First'.
Re: Making a hash of arrays from a while loop
by moritz (Cardinal) on Mar 04, 2008 at 09:30 UTC
    Storing @array in a hash %hash with $key as the key is a simple matter of one line:
    $hash{$key} = \@array;
      Yes, but it doesn't store the values, (as far as I can tell---otherwise I'll post my code and see what else stupid I am doing wrong!) it stores a reference to the array, so when the array is recalculated, doesn't the reference point to the new value of the array?
      ........
      Those are my principles. If you don't like them I have others.
      -- Groucho Marx
      .......
        You're right, and the workaround is to create a new array:
        $hash{$key} = [ @array ];

        If there are references inside the array, you'll need somthing like Storable::dclone.

        Or declare @array within the while loop. This avoids making a copy of the array to make the anonymous reference.
        #!/usr/bin/perl use strict; use warnings; use Data::Dumper; $Data::Dumper::Indent = 1; my %hash; while (my $rec = <DATA>){ chomp $rec; # declare the array my ($key, @array) = split(/ /, $rec, 2); $hash{$key} = \@array; } print Dumper \%hash; __DATA__ 1 2 3 4 5 6 7 8
        $VAR1 = { '1' => [ '2 3 4' ], '5' => [ '6 7 8' ] };

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-03-29 12:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found