anonymous hash slices
Just as an academic exercise, I thought I should be able to do this:
**************************************
[at] a=(l=>35,k=>31,r=>7,k=>6);
[at] r=qw/l r r k/;
# make an anonymous hash using [at] a, then grab values from it using [at] r as keys
[at] a={ [at] a}{ [at] r};
print join(",", [at] a), "\n"'
**************************************
.... but it doesn't like the {}{} notation. Is this not possible? I do this
with lists all the time, e.g. [at] a = (split)[1,7,15];
TIA.
- Bryan
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: anonymous hash slices
>>>>> "BRH" == Bryan R Harris <Bryan_R_Harris [at] raytheon.com> writes:
BRH> Just as an academic exercise, I thought I should be able to do this:
BRH> **************************************
BRH> [at] a=(l=>35,k=>31,r=>7,k=>6);
BRH> [at] r=qw/l r r k/;
try to use some white space in your code. even in short examples it
helps.
BRH> # make an anonymous hash using [at] a, then grab values from it using
BRH> [at] r as keys
BRH> [at] a={ [at] a}{ [at] r};
learn the first rule of dereferencing. take a reference and wrap it in
{} with the proper sigil prefix. so to deref a hash, wrap it in %{}.
so you are making an anon hash around [at] a but not dereferencing it. you
need %{{ [at] a}}.
then to take a slice of that, you would change the % to [at] :
[at] {{ [at] a}}{ [at] r}
BRH> ... but it doesn't like the {}{} notation. Is this not possible?
BRH> I do this with lists all the time, e.g. [at] a = (split)[1,7,15];
that isn't the same. that is a slice of a list. you had an anon hash
which must be dereferenced first before you can slice it.
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/