develooper Front page | perl.beginners | Postings from February 2002

RE: Hash Question

Thread Previous | Thread Next
Hanson, Robert
February 1, 2002 10:04
RE: Hash Question
Message ID:
Just one unless you use references.  

Here is an example with references...

%hash = ( one => 1, two => 2 );
$hash{oneagain} = \$hash{one};

foreach my $key ( keys %hash ) {
	my $value = (ref $hash{$key}) ? ${$hash{$key}} : $hash{$key};
	print "$key => $value\n";

....And when you change the value of either key (directly or by reference)
both values will be changed because they point to the same piece of data.
And if you delete one of the keys the other will still remain.

You can change it by setting up a sub like this (or directly but that would
be messy)...

setVal(\%hash, 'one', 'ONE');
setVal(\%hash, 'oneagain', 'ONE');

sub setVal {
	my $hash = shift;
	my $key = shift;
	my $value = shift;
	if (ref $hash->{$key}) {
		${$hash->{$key}} = $value;
	else {
		$hash->{$key} = $value;


-----Original Message-----
From: Balint, Jess []
Sent: Friday, February 01, 2002 12:43 PM
To: ''
Subject: Hash Question

Since this is a beginners list, I thought I would be allowed to ask this
question. Can there be multiple values for hash keys, or just one? The
reason I am asking is that I am working on a statistical program and would
need to use multiple hashes as values of another hash. If this is possible,
please let me know. Thank you.


To unsubscribe, e-mail:
For additional commands, e-mail:

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About