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

Re: I need hash inside a hash help

Thread Previous | Thread Next
ToddAndMargo via perl6-users
January 11, 2019 19:58
Re: I need hash inside a hash help
Message ID:
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 Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About