Reputation: 47
I'm trying to put a hash %listvol
into an array @fileInfo
in Perl.
@fileInfo = ($filename, $data, $index, \%listvol);
%listvol
contains a list of volume: key = $vol
, value = $vol
.
The first $vol
values are ABCDEF
, then GFFFF
, EEEAA
- always different.
Then I put the array @fileInfo
in the hash %listeAllFile
:
$listeAllFile{$nameOfFile} = [@fileInfo];
Later I'm trying to get the hash %listvol
without success. I'm using this code:
foreach $key (keys %listeAllFile) {
@tab = @{ $listeAllFile{$key} };
$filename = $tab[0];
%listvol = %{ $tab[3] };
foreach $vol (keys %listvol) {
print "\n vol is $vol for file $filename";
}
The file name is always different, so it is ok. But the value of the variable $vol
is always the same, ABCDEF
. It seems that I get get each time the same value.
Does anyone have an idea?
Upvotes: 0
Views: 93
Reputation: 62099
While you didn't include code to reproduce your problem, I'm fairly sure that the issue is that you're storing a reference to the same %listvol
hash in each array.
When you change the contents of %listvol
for the second entry, you're modifying the first entry at the same time. One way to fix that is to use {%listvol}
instead of \%listvol
. The former makes a shallow copy of the current contents of %listvol
, just like [@fileInfo]
makes a shallow copy of the current contents of @fileInfo
.
Upvotes: 2