Perl newb here, sorry for a silly question, but googling ->
for a coding context is tough... Sometimes, I will access a hash like this: $hash{key}
and sometimes that doesn't work, so I access it like this $hash->{key}
. What's going on here? Why does it work sometimes one way and not the other?
A hash is a set of key/value pairs. Hash variables are preceded by a percent (%) sign. To refer to a single element of a hash, you will use the hash variable name preceded by a "$" sign and followed by the "key" associated with the value in curly brackets..
A Hash is declared using my keyword. The variable name is preceded by the dollar sign($)followed by key under curly braces and the value associated with the key. Each key is associated with a single value.
Extracting Keys and Values from a Hash variable The list of all the keys from a hash is provided by the keys function, in the syntax: keys %hashname . The list of all the values from a hash is provided by the values function, in the syntax: values %hashname . Both the keys and values function return an array.
The easiest way to tell what's in there is: use Data::Dumper; print Dumper $HashVariable; Assuming this is a hash reference - which it would be, if print $HashVariable gives HASH(0xdeadbeef) as an output.
Perl-Hashes. A hash is a set of key/value pairs. Hash variables are preceded by a percent (%) sign. To refer to a single element of a hash, you will use the hash variable name preceded by a "$" sign and followed by the "key" associated with the value in curly brackets..
Ok, but standard hashes in Perl can definitely not have duplicate keys. The keys from your example are all different. "$BirthDate|...|1" is different from "$BirthDate|...|2", isn't it? So the keys are not duplicate. PerlDuck, the sequence number makes the key unique. If I have a Unique Primary Key let's say Social Security Number.
It’s like building up a record that itself contains a group of other records. The format for creating a hash of hashes is similar to that for array of arrays. Simply, instead of assigning the values to the primary keys in a normal hash, assign a whole hash containing secondary keys and their respective values to the primary keys of the outer hash.
@Jake T., So? Seeing as crypto hashes have neither keys nor values, there is no confusion in the OP's request. Furthermore, the context was Perl-specific. it can have duplicate values but not keys. For both hashes and arrays, only one scalar can be stored at a given key. ("Keys are unique.") If they weren't, you couldn't do
The difference is that in the first case %hash
is a hash, and in the second case, $hash
is a reference to a hash (= hash reference), thus you need different notations. In the second case ->
dereferences $hash
.
EXAMPLES:
# %hash is a hash:
my %hash = ( key1 => 'val1', key2 => 'val2');
# Print 'val1' (hash value for key 'key1'):
print $hash{key1};
# $hash_ref is a reference to a hash:
my $hash_ref = \%hash;
# Print 'val1' (hash value for key 'key1', where the hash
# in pointed to by the reference $hash_ref):
print $hash_ref->{key1};
# A copy of %hash, made using dereferencing:
my %hash2 = %{$hash_ref}
# $hash_ref is an anonymous hash (no need for %hash).
# Note the { curly braces } :
my $hash_ref = { key1 => 'val1', key2 => 'val2' };
# Access the value of anonymous hash similarly to the above $hash_ref:
# Print 'val1':
print $hash_ref->{key1};
SEE ALSO:
perlreftut: https://perldoc.perl.org/perlreftut.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With