Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Assignment of Arrays of Hashes

by Tanalis (Curate)
on Oct 14, 2002 at 09:26 UTC ( [id://205011] : perlquestion . print w/replies, xml ) Need Help??

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

Heys all,

Bit of a stupid question, probably, but it's driving me mad .. I have a hash of data which I'm trying to assign to an array using

push @RefHash, \%content;

Now, I've checked that I have clean data up to that point, and that %content is getting populated correctly, and it is.

It seems that the push simply isn't working - anyone have any ideas why this might be the case?

The assignment code etc. is below:

my @selector = ("field1", "field2", "field3", "field4"); open IN, $FILE; my @RefHash; while (<IN>) { chomp; my @inLine = split; my %content; @content{@selector} = @inLine; push @RefHash, \%content; }

Like I say, it's probably something stupid, but I can't see it ..

Thanks in advance

Replies are listed 'Best First'.
Re: Assignment of Arrays of Hashes
by broquaint (Abbot) on Oct 14, 2002 at 09:48 UTC
    Look at that split() very carefully ... Here's a slightly modified working version of your code
    use Data::Dumper; my @selector = ("field1", "field2", "field3", "field4"); my @RefHash; while (<DATA>) { chomp; my @inLine = split /,/; my %content; @content{@selector} = @inLine; push @RefHash, \%content; } print Dumper \@RefHash; __END__ foo,bar,baz,quux one,two,three,four ichi,ni,san,shi



    update: uncaught exception ENOCAFFENE thrown

Re: Assignment of Arrays of Hashes
by Aristotle (Chancellor) on Oct 14, 2002 at 11:41 UTC
Re: Assignment of Arrays of Hashes
by physgreg (Scribe) on Oct 14, 2002 at 09:55 UTC
    I modified the script to use Data::Dumper after the assignment to @content{@selector} and after the loop. Using the test data
    test1 test2 test3 test4 test44 afsd wfsdf sdfsd
    I got the output
    $VAR1 = 'field1'; $VAR2 = 'test1'; $VAR3 = 'field2'; $VAR4 = 'test2'; $VAR5 = 'field3'; $VAR6 = 'test3'; $VAR7 = 'field4'; $VAR8 = 'test4';
    after the first assignment, so the hash is indeed being set up correctly. After the loop, my RefHash was set to
    $VAR1 = { 'field1' => 'test1', 'field2' => 'test2', 'field3' => 'test3', 'field4' => 'test4' }; $VAR2 = { 'field1' => 'test44', 'field2' => 'afsd', 'field3' => 'wfsdf', 'field4' => 'sdfsd' };
    so it appears that the push worked. Perhaps your problem is to do with the way that you are trying to access the array elements? I was able to access it with
    print $RefHash[0]{'field2'},"\n";
Re: Assignment of Arrays of Hashes
by Tanalis (Curate) on Oct 14, 2002 at 09:59 UTC
    Nyah, sorted.
    my in the wrong place wasn't clearing the hash first :/

    Oh well, cheers for your help anyway.

Re: Assignment of Arrays of Hashes
by blm (Hermit) on Oct 14, 2002 at 09:46 UTC

    I read your code and think that you mean something like

    $content{$selector[1]} = \@inline;

    When you use %hash you say eg$hash{'key'} = "Something". When you use an array eg @array you use eg $array[1] = "Something".

    In case you don't know putting the following at the top of your scripts turns on useful warnings that help you catch problems quickly.

    use strict; use warnings;
    Update: OK, OK! I had not seen anywhere that you could do @hash{'key1', 'key2', 'key3'} = ('value1', 'value2', 'value3'); I am sorry! See Aristotle reply for explanation --blm--
      No, he is using the hash correctly. You can assign to multiple hash keys at once by saying @hash{'key1', 'key2', 'key3'} = ('value1', 'value2', 'value3');. This is known as a "slice", and that's what he's doing.

      Makeshifts last the longest.