Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Making a hash of arrays from a while loop

by why_bird (Pilgrim)
on Mar 04, 2008 at 09:27 UTC ( #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
  • Comment on Making a hash of arrays from a while loop

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?

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2023-06-09 09:04 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (35 votes). Check out past polls.