Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding multiple value to a hash 4

Status
Not open for further replies.

thendal

Programmer
Aug 23, 2000
284
Hi all, How to add a array of value to a hash

tried something like this ..didn't work

I have a array @values=("one","two");
$add_hash{values}=@values;


When i retrive $add_hash{values} it didn't output array value..

I guess i am missing something here...?


 
Hashes can only store scalars as their values. This means that when you want add an array as a hash value, you have to use a reference to the array rather than the array itself:
Code:
$add_hash{values} = \@values;

To read more on datastructures like this, have a look at perllol.
 
maybe the following lines will help...

Code:
# Setup hash with array elements
my $hash = { key => [ "a", "b", "c" ] };
# Display complete array
print "@{$hash->{key}}\n";
# Display first element
print "@{$hash->{key}}[0]\n";
 
just for a further explanation, since I already started typing this..

You can only store scalars in a hash, so what's happening is you're getting the scalar value of that array which is '2'
To do what you want, you need to add a reference to the array (a reference is a scalar)

Code:
@values_01=("one","two");
$ref_values_01 = \@values_01;
$add_hash{values_01}=$ref_values_01;

print $add_hash{values_01}->[0];
print $add_hash{values_01}->[1];
You can simplify this by putting brackets around the array instead of creating the reference ahead of time:
Code:
@values_01=("one","two");
$add_hash{values_01}=[@values_01];

print $add_hash{values_01}->[0];
print $add_hash{values_01}->[1];
(You probably don't want to use "values" as a key or variable name because it's a perl keyword.)
 
pretty sure this will work too:

I have a array @values=("one","two");
$add_hash{values}=[@values];


 
The difference between
Code:
$add_hash{values}=\@values;
and
Code:
$add_hash{values}=[@values];
is that the second form ([]) makes a copy of @values as it exists at that point in your program. Later changes to @values will not be reflected in the hash. If you use the first form (\), later changes in @values will be reflected in the hash. So which to use depends upon what you want to happen.

Before I understood the difference, I often shot myself in the foot with the first version.

 
Very good point Mike. I wasn't sure of the difference until you pointed that out.
 
its a good point, but it's also the same regardless of if using references, anonymous data storage, or not. Anytime you make a copy of an array and work with the copy the original array is unaffected.

I think where people also get confused is passing arrays (or lists) to subs and thinking the passed in array will not affect the original array, like in this trivial example:

Code:
my @array = (1..10);
increment(@array);
print "@array";
sub increment {$_++ for @_;}

you have to make a copy of @array of you don't want it changed. Or I have seen this numerable times on forums:

Code:
my @array = (1..10);
increment(@array);
print "@array";
sub increment {
  my @temp = @_;
  $_++ for @temp;
  return @temp;
}

then the questioner wants to know why @array hasn't been changed.
 
perldoc perldsc, the section COMMON MISTAKES is good reading on the difference between [] and \.


 
\ vs []

Don't you just love the self-documenting nature of Perl code?

Mike

You cannot really appreciate Dilbert unless you've read it in the
original Klingon.

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top