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

Re: I need hash inside a hash help

Thread Previous | Thread Next
From:
Bruce Gray
Date:
January 11, 2019 19:59
Subject:
Re: I need hash inside a hash help
Message ID:
1D4C3462-B992-46E5-827D-F47EC9A3F1A6@acm.org
That may work, but is bad practice.
Instead of:
	%Vendors<< $x >><ContactName>
, please use:
	%Vendors{$x}<ContactName>

The relation of `<<…>>` is to `<…>` as double-quotes are to single-quotes; doubling up changes from non-interpolating to interpolating.

To say `%Vendors<< $x >>` is to take the shortcut intended for constant keys, and bludgeon it to make it support variable keys, when the basic (and shorter) syntax of `%Vendors{$x}` handles the variable key naturally.

— 
Hope this helps,
Bruce Gray (Util of PerlMonks)


> On Jan 11, 2019, at 1:50 PM, ToddAndMargo via perl6-users <perl6-users@perl.org> wrote:
> 
> On 1/11/19 11:43 AM, ToddAndMargo via perl6-users wrote:
>> On 1/11/19 11:39 AM, ToddAndMargo via perl6-users wrote:
>>> On 1/11/19 11:33 AM, JJ Merelo wrote:
>>>> I think you want $x, not $Ace.
>>>> 
>>>> Cheers
>>> 
>>> Yup.  I am on fire today!  :'(
>>> 
>>> Still can't get it figured out.  :'(  :'(
>>> 
>>> $ p6 'my $x="Ace"; my %Vendors=("acme" => { "ContactName" => "Larry", "AccountNo" => 1234 }, "Ace" => { "ContactName" => "Mo", "AccountNo" => "A102" } ); say "%Vendors<$x><ContactName>" ~ "\t" ~ "%Vendors<$x><AccountNo>";'
>>> Use of uninitialized value of type Any in string context.
>>> 
>>> 
>>> $ p6 'my $x="Ace"; my %Vendors=("acme" => { "ContactName" => "Larry", "AccountNo" => 1234 }, "Ace" => { "ContactName" => "Mo", "AccountNo" => "A102" } ); say "%Vendors<"$x"><ContactName>" ~ "\t" ~ "%Vendors<"$x"><AccountNo>";'
>>> Use of uninitialized value of type Any in string context.
>>> 
>>> $ p6 'my $x="Ace"; my %Vendors=("acme" => { "ContactName" => "Larry", "AccountNo" => 1234 }, "Ace" => { "ContactName" => "Mo", "AccountNo" => "A102" } ); say "%Vendors<{$x}><ContactName>" ~ "\t" ~ "%Vendors<{$x}><AccountNo>";'
>>> Use of uninitialized value of type Any in string context.
>>> 
>>> $ p6 'my $x="Ace"; my %Vendors=("acme" => { "ContactName" => "Larry", "AccountNo" => 1234 }, "Ace" => { "ContactName" => "Mo", "AccountNo" => "A102" } ); say "%Vendors<{"$x"}><ContactName>" ~ "\t" ~ "%Vendors<{"$x"}><AccountNo>";'
>>> Use of uninitialized value of type Any in string context.
>>> 
>>> 
>>> I can't win.
>> $ p6 'my $x="Ace"; my %Vendors=("acme" => { "ContactName" => "Larry", "AccountNo" => 1234 }, "Ace" => { "ContactName" => "Mo", "AccountNo" => "A102" } ); say "%Vendors<<$x>><ContactName>" ~ "\t" ~ "%Vendors<<$x>><AccountNo>";'
>> ===SORRY!===
>> Unable to parse expression in double quotes; couldn't find final '"' (corresponding starter was at line 1)
>> at -e:1
>> ------> >" ~ "\t" ~ "%Vendors<<$x>><AccountNo>";⏏<EOL>
>>     expecting any of:
>>         double quotes
>>         postfix
>> Other potential difficulties:
>>     Ambiguous use of >>; use » instead to mean hyper, or insert whitespace before >> to mean a quote terminator (or use different delimiters?)
>>     at -e:1
>>     ------> 2" } ); say "%Vendors<<$x>><ContactName>⏏" ~ "\t" ~ "%Vendors<<$x>><AccountNo>";
>>     Ambiguous use of >>; use » instead to mean hyper, or insert whitespace before >> to mean a quote terminator (or use different delimiters?)
>>     at -e:1
>>     ------> me>" ~ "\t" ~ "%Vendors<<$x>><AccountNo>⏏";
> 
> 
> I got it finally.  I had to switch from a one liner to an actual program
> 
> 
> <code HashOfHashTest.pl6>
> #! /usr/bin/env perl6
> 
> my $x       = "Ace";
> my %Vendors = ( "acme" => { "ContactName" => "Larry", "AccountNo" => 1234 },
>                "Ace" =>  { "ContactName" => "Mo",    "AccountNo" => "A102" } );
> 
> print( %Vendors<< $x >><ContactName> ~ "\t" ~ %Vendors<< $x >><AccountNo> ~ "\n" );
> <M/code>
> 
> 
> $ HashOfHashTest.pl6
> Mo	A102
> 
> And it demanded a white space in << $x >>

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