develooper Front page | perl.perl6.users | Postings from September 2017

Tip: assign a value to a hash using a variable as a key

Thread Next
From:
ToddAndMargo
Date:
September 30, 2017 07:25
Subject:
Tip: assign a value to a hash using a variable as a key
Message ID:
63f9783e-a18a-1dba-8ae1-796d9d285c1e@zoho.com
Hi All,

This one about killed me to find!  The guys on the chat line
figured it out for me.  Apparently some tutorial is wrong on
this too, but I couldn't figure out which one they were
referring to.

When assigning a value to a hash using a variable as the key,
use {} instead of <>


perl6 -e ' my %x = ( "a" => "A", "b" => "x", "c" => "C" );
            my $y="b";
            %x{$y}="BB"; say %x;'

{a => A, b => BB, c => C}


Wow.  I never would have guessed that one!
-T

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