$hash{"camel"} = "filters";
$hash{"abcde"} = "alphabet";
$hash{"xyz"} = "variables";
$hash{123} = "numbers";
%hash = (123, "numbers", "abcde", "alphabet", "camel", "filters",
"xyz", "variables"};
%hash = qw (123 numbers camel filters abcde alphabet xyz variables);
%hash = (123 => "numbers",
abcde => "alphabet",
camel => "filters",
xyz => "variables"};
%hash, use of the (scalar)
elements employs a $ (as always) together with braces.$hash{"abcde"}
$hash{120+3}
@array = %hash;
print "@array\n";
The order of elements in the unwound array is indeterminate and bears little if any meaning.
%newhash = reverse %oldhash).
%oldhash is converted into an array because it
appears in an array context.
%newhash is assigned a new value from the
reversed array. This maps values to their corresponding
keys.
Moral of this story: we can only invert a one-to-one function.
keys function returns the list of all keys that
currently appear in the hash table.
As you already know, parentheses are optional when calling
built-in functions? Thus keys(%hash) means the same
thing as keys %hash.
%hash) in a scalar context
yields value 0 if and only if the hash is empty.
values function return for a function.Note: keys and values return corresponding elements in the same order.
each to iterate over the key-value pairs in
a hash unless you have a good reason not to.
while (($key,$value) = each(%hash)) {
print "key $key has value $value\n";
}
This allows you to get at each of the key-value pairs without having to worry about what order to put them in.
foreach $key (sort(keys %hash)) {
print "key $key has value $hash{$key}\n";
}
$weight_record{"boat"} = 155000000;
$weight_record{"car"} = 12345;
$weight_record{"man"} = 697;
@weight_record{"boat","car","man"} = (155000000, 12345, 697);
@weight_record{keys %new_records} = values %new_records;
%weight_records = (%weight_records, %new_records);
Why is it likely to be slower than
@weight_record{keys %new_records} = values %new_records;?
print scrolling_list (
-name => "flavors",
-values => [ qw (mint chocolate cherry vanilla peach) ],
-labels => {
mint => "Mighty Mint",
chocolate => "Cherished Chocolate",
cherry => "Cherry Cherry",
vanilla => "Very Vanilla",
peach => "Perfectly Peachy",
},
-size => 3,
-multiple => 1,
);
The -labels argument of the scrolling list maps variable values (that will be sent to the CGI target) into the labels that will be displayed on the web page the uuser sees.
What the heck are the brackets all about?
They
construct a reference to an array constructed from
the list of elements contained within them.
And what about the braces around { mint => "Mighty Mint
... }?
They construct a reference to a hash.
And why are there those extra commas after the "Perfectly
Peachy" and -multiple => 1?
They are perfectly acceptable and allow one to add an element
without forgetting to put in the comma.
It's a scalar value that tells where the value of another object can be found.
If you think of all of memory as being an array, then a pointer is an index into that array of where something starts.
A reference to a variable is created by using the backslash operator \.
Thus, if the hash %flavors is stored starting at
array element 6,150. A reference to that hash,
\%flavors, is a scalar of type reference
with value 6,150.
If you try to use a reference, you must dereference it, by specifying an extra dollar sign. This works as follows:
$x = 123; $x_reference = \$x; $$x_reference = 456; print "$$x_reference\n"; print "$x\n";
What gets printed by the above code?
%flavors = (
mint => "Mighty Mint",
chocolate => "Cherished Chocolate",
cherry => "Cherry Cherry",
vanilla => "Very Vanilla",
peach => "Perfectly Peachy",
);
print scrolling_list (
-name => "flavors",
-values => [ keys %flavors ],
-labels => \%flavors,
-size => 3,
-multiple => 1,
);
In what order do you think the labels be printed? You'd better find out if you want to use this.