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

RE: Hash Question

Thread Previous | Thread Next
From:
Hanson, Robert
Date:
February 1, 2002 10:04
Subject:
RE: Hash Question
Message ID:
11A304418ECBAB498FBF10ABCEA5E499D1CDE1@hq2kax1.nj.aptegrity.com
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;
	}
}

Rob


-----Original Message-----
From: Balint, Jess [mailto:JBalint@alldata.net]
Sent: Friday, February 01, 2002 12:43 PM
To: 'beginners@perl.org'
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.

-Jess

-- 
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About