[Setup-tool-hackers] Little Perl complex struct tutorial.
- From: Arturo Espinosa Aldama <arturo mezcal dyndns org>
- To: Michael Meeks <michael ximian com>
- Cc: <setup-tool-hackers helixcode com>
- Subject: [Setup-tool-hackers] Little Perl complex struct tutorial.
- Date: Tue, 27 Mar 2001 13:14:26 -0600 (CST)
I didn't commit any changes yesterday because I didn't finish the frontend
interaction, and thus everything would get broken, so the new code is not
yet in.
About the hashes, it's quite trivial since Perl 5. Are you dealing with a
declaration or just manual assignment? The hint here is that a hash or an
array can only contain scalar values. Scalars are either strings or
references, so what you have to do is store a reference to what you want
to store.
Perl does ref counting and garbage collecting, so you don't have to worry
much about dangling refs and stuff like that.
When dealing with declarations, the only thing you need to know is that []
and {} are operators that return references to copies of the surrounded
arrays and hashes, respectively. So, if you want to delare a complex hash,
you can do something like this:
my %hash =
(
"hello", { "english", "hi",
"italian", "ciao"
"misc", [ "more", "stuff", 29, 117 ] },
"goodbye", ["goodbye", "ciao", "sayonara"]
);
Or, for syntactic sugar:
my %hash =
(
"hello" => { "english" => "hi",
"italian" => "ciao",
"misc" => [ "more", "stuff", 29, 117 ] },
"goodbye" => ["goodbye", "ciao", "sayonara"]
);
Both expressions create a local hash %hash, which has two keys, "hello"
and "goodbye", which have references to a hash and an array, respectively.
Note that $other_hash = { %hash }; would make $other_hash a ref to a copy
of the original hash: not a ref of the same hash (use \%hash to just get a
ref). Still, the copy is shallow, so you would be talking about the same
hash if you did ${$other_hash}{"hello"} (or for sugar,
$$other_hash{"hello"}). Read on to understand the notation.
To access the value "hi":
${$hash{"hello"}}{"english"};
or
%h = %{$hash{"hello"}};
$h{"english"};
To access the second value in the array:
${$hash{"goodbye"}}[2];
or
@a = @{$hash{"goodbye"}};
$a[2];
So @{ref} and %{ref} unref references, and thus (consistent with the
$hash{} and $array[] notations), ${ref}{key} and ${ref}[n] obtain elements
from referenced hashes and arrays, respectively. It is possible to do
$$my_var{"elem"} or $$my_var[2] if $my_var is a ref to a hash or an array.
The compound statements in the last examples are not completly the same as
the others, because an assignment from one array to other, or one hash to
other, copies the array or hash, in the same way as [] or {}, but without
referencing. It's just there for explanatory purposes, but I wouldn't
recommend using it too often, or for large structures, unless what you
want is a copy.
As a last note, my @a = @b; $my_ref = \@a; is the same as $my_ref = [ @a
];
Greetings,
Arturo
_______________________________________________
setup-tool-hackers maillist - setup-tool-hackers@helixcode.com
http://lists.helixcode.com/mailman/listinfo/setup-tool-hackers
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]