Front page | perl.beginners |
Postings from February 2002
RE: Hash Question
Thread Previous
|
Thread Next
From:
Hanson, Robert
Date:
February 1, 2002 10:29
Subject:
RE: Hash Question
Message ID:
11A304418ECBAB498FBF10ABCEA5E499D1CDE4@hq2kax1.nj.aptegrity.com
Better yet you could use 'tie' and create your own hash implementation like
the code below. You will be able to set and retreive values normally except
for the fact that if you want a single value to be linked to two keys you
will need to store the value in one key and reference that key in another
key (i.e. $hash{foo} = 1; $hash{bar} = \$hash{foo}).
Rob
package MyHash;
require Tie::Hash;
@ISA = (Tie::StdHash);
sub FETCH {
my $hash = shift;
my $key = shift;
if (ref $hash->{$key}) {
return ${$hash->{$key}};
}
else {
return $hash->{$key};
}
}
sub STORE {
my $hash = shift;
my $key = shift;
my $value = shift;
if (ref $hash->{$key}) {
${$hash->{$key}} = $value;
}
else {
$hash->{$key} = $value;
}
}
package main;
tie %hash, 'MyHash';
%hash = ( one => 1, two => 2 );
$hash{oneagain} = \$hash{one};
$hash{three} = 3;
$hash{oneagain} = 'ONE';
foreach my $key ( keys %hash ) {
print "$key => $hash{$key}\n";
}
-----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
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
Thread Previous
|
Thread Next