Arrays of hashes? (A duplicate function needed???) 
Author Message
 Arrays of hashes? (A duplicate function needed???)

Hi, I am trying to build an array of hashes using references but seem to
end up with duplicate references. The following code should demonstrate...

%list = {name=>"Name$i",
         tag=>"Constant",
         value=>"$i"
         };

for($i = 0; $i < 10; $i++){
    $list{value} = $i;
    $list{name}="Name$i";
    $lista[$i]=\%list;

Quote:
}

print "Method1:\n";
for($i = 0; $i < 10; $i++){
    $listpt = $lista[$i];
    print "$$listpt{name} $$listpt{value}\n";
Quote:
}

print "\nMethod2:\n";
for($i = 0; $i < 10; $i++){
    $listb[$i]={name=>"Name$i",
               value=>"$i"
               };
Quote:
}

for($i = 0; $i < 10; $i++){
    print "$listb[$i]->{name} $listb[$i]->{value}\n";

Quote:
}

This produces:
Method1:
Name9 9
Name9 9
Name9 9
Name9 9
Name9 9
Name9 9
Name9 9
Name9 9
Name9 9
Name9 9

Method2:
Name0 0
Name1 1
Name2 2
Name3 3
Name4 4
Name5 5
Name6 6
Name7 7
Name8 8
Name9 9

I would like to be able to code it via method one, or at least something
similar, but I haven't figured out how to duplicate a hash array.

I am also slightly confused about method2, since it seems that $listb[$i]
suddenly becomes a hash array, but yet you cannot always treat it like that.

This is in perl5 using what I think is the last 5.000 release.

Any suggestions would be gratefully received...

Sam.

--
Sam Richards         |  Tel: 914-941-5260   |   Blue Sky Productions




Fri, 02 May 1997 05:30:41 GMT  
 Arrays of hashes? (A duplicate function needed???)

Quote:

>Hi, I am trying to build an array of hashes using references but seem to
>end up with duplicate references. The following code should demonstrate...

>%list = {name=>"Name$i",
>     tag=>"Constant",
>     value=>"$i"
>     };

>for($i = 0; $i < 10; $i++){
>    $list{value} = $i;
>    $list{name}="Name$i";
>    $lista[$i]=\%list;
>}

>print "Method1:\n";
>for($i = 0; $i < 10; $i++){
>    $listpt = $lista[$i];
>    print "$$listpt{name} $$listpt{value}\n";
>}
>print "\nMethod2:\n";
>for($i = 0; $i < 10; $i++){
>    $listb[$i]={name=>"Name$i",
>           value=>"$i"
>           };
>}
>for($i = 0; $i < 10; $i++){
>    print "$listb[$i]->{name} $listb[$i]->{value}\n";
>}

>This produces:
>Method1:
>Name9 9
>Name9 9
>Name9 9
>Name9 9
>Name9 9
>Name9 9
>Name9 9
>Name9 9
>Name9 9
>Name9 9

>Method2:
>Name0 0
>Name1 1
>Name2 2
>Name3 3
>Name4 4
>Name5 5
>Name6 6
>Name7 7
>Name8 8
>Name9 9

>I would like to be able to code it via method one, or at least something
>similar, but I haven't figured out how to duplicate a hash array.

>I am also slightly confused about method2, since it seems that $listb[$i]
>suddenly becomes a hash array, but yet you cannot always treat it like that.

It becomes a *reference* to a hash.  You must dereference it to behave
like a hash.  Perl never does any implicit dereferencing for you.

Quote:
>This is in perl5 using what I think is the last 5.000 release.

>Any suggestions would be gratefully received...

With Method1 you are using the same global variable %list throughout
the loop, so all the ref's you are generating refer to the same thing.
Add `my %list' first thing inside the for loop.  This creates a new
variable each time through, so your code will behave as you expect.

With Method2 you are creating a (new) anonymous hash reference within
the loop, so you are safe.

Anno



Fri, 02 May 1997 20:02:08 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. URGENT: Hash in Hash, Array in Array, Array in Hash, Hash in Array

2. removing duplicates from an array of hashes

3. Sorting hash of Arrays now drops duplicate keys.

4. Need help with perl structure (hash of arrays and hashes)

5. hash of values and array of hashes of values and array of hashes

6. array/hash to file to array/hash

7. Simple Perl5 question (hash-array of hash-array refs)

8. question regarding hash hash hash of array

9. Help with perl hashes of hashes and arrays of hashes etc

10. baby steps with complex hashes of hashes of hashes of arrays

11. Help needed in cheking/improving a perl script involving 2-d arrays/hashes

12. help needed for array of hashes comparison

 

 
Powered by phpBB® Forum Software