[ABE.pm] notes from post-meeting
Ricardo SIGNES
rjbs-perl-abe at lists.manxome.org
Thu Jul 12 07:49:24 PDT 2007
* "Faber J. Fedor" <faber at linuxnj.com> [2007-07-12T09:58:58]
>
> my %f = %{$Messages{$id}};
>
> Is this what you call a hash slice?
Nope, that's just plain ol' list assignment to a hash.
You can always write this:
my %hash = (
one => 1,
two => 2,
);
That's a nice way to format:
my %hash = ("one", 1, "two", 2);
...because when you assign a list of values to a hash, it makes each pair of
entries into a key/value pair. (That's why you'd get a warning if you assigned
an odd set like this: %hash = (1, 2, 3, 4, 5).)
In list context, a hash evaluates to its names and values. So:
my %hash = (one => 1, two => 2);
my @array = %hash;
Now @array is ("one", 1, "two", 2);
Put these two together and you can say:
%new_hash = %old_hash;
and it does what you probably mean. Note that it replaces ALL content
currently in %new_hash.
A hash slice represents a subset of the values of the hash:
my %hash = (
one => 1,
two => 2,
tre => 3,
);
my @values = @hash{ "one", "two" };
Now @values contains (1, 2); Note that the % on %hash becomes a @ for a slice,
not a $ like a single-value lookup.
You can assign to slices:
@hash{ "four", "five" } = (4, 5);
...and now your hash has five entries, because the entries for one, two, tre
are not replaced. That's a large way in which assigning to a slice differs
from assigning to the whole hash.
--
rjbs
More information about the ABE-pm
mailing list