develooper Front page | perl.perl6.users | Postings from January 2019

Re: I need hash inside a hash help

Thread Previous | Thread Next
From:
ToddAndMargo via perl6-users
Date:
January 11, 2019 19:58
Subject:
Re: I need hash inside a hash help
Message ID:
93d128ee-306b-fb4b-2cd8-077ff3d82209@zoho.com
On 1/11/19 11:45 AM, Bruce Gray wrote:
> Short answer: use `%hash{$var}`, not `%hash<$var>`.
> 
> When they are not in position to be less-than and greater-than comparison operators, the pair of left and right angle brackets are a circumfix operator that work like Perl 5’s “quote word” op: `qw()`.
> 
> In Perl 6, `<>` are used a lot, including as a shortcut in hash lookups.
> The full form for looking up the constant key `acme` in %Vendors is to use curly braces and to*quote*  the key (single or double quote), or have the key in a variable:
> 	say %Vendors{'acme’};
> 	say %Vendors{"acme”};
> 	my $k = ‘acme’;
> 	say %Vendors{$k};
> The shortcut of replacing the curly braces with angle brackets only works for constant strings:
> 	say %Vendors<acme>;
> 
> Advanced note: Since `<>` produce a*list*  of quoted words, you can use them to extract multiple values from a hash:
> 	my ( $acct, $cn ) = %Vendors{"acme"}{"AccountNo", "ContactName”};
> 	my ( $acct, $cn ) = %Vendors<acme><AccountNo ContactName>;
> 	say [:$acct, :$cn].perl;


Yes it does help.  I copied it to my hash Keepers file.  Thank you!

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About