On 09/30/2017 02:20 AM, Timo Paulssen wrote:
This doc page should help you a lot with this topic:
https://docs.perl6.org/language/subscripts#Basics
A little bit. I had found that when I was researching. It did not
go into who to use a variable as a key value.
This is what I wrote myself:
<perl6.hashes.txt>
Perl 6 Hashes (associative arrays):
References:
https://docs.perl6.org/language/subscripts#Basics
https://docs.perl6.org/type/Hash#:exists
Creating and populating:
my %y = ( 'aa'=>"AA", 'bb'=> "BB", 'cc'=>"CC", 'dd'=>"DD" );
$ perl6 -e 'my %x = (aaa => "x", b=>"y", c=>"z"); say %x;'
{aaa => x, b => y, c => z}
$ perl6 -e 'my %x = aaa => "x", b=>"y", c=>"z"; say %x;'
{aaa => x, b => y, c => z}
$ perl6 -e 'my %x = [aaa => "x", b=>"y", c=>"z"]; say %x;'
{aaa => x, b => y, c => z}
Assigning an array to the hash
$ perl6 -e 'my %x = [aaa => "x", b=>@["y","q","r"], c=>"z"]; push
@(%x<b>), "eeee"; say %x; for @(%x<b>) {say $_};'
{aaa => x, b => [y q r eeee], c => z}
y
q
r
eeee
Assigning a value based on a varaible as a 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}
Altering a value:
%x<a> = "abc";
$ perl6 -e 'my %x = [aaa => "x", b=>"y", c=>"z"]; %x<a> = "abc"; say %x;'
{a => abc, aaa => x, b => y, c => z}
Printing/reading a value:
my $y=%x<b>;
say %x<b>;
$ perl6 -e 'my %x = [aaa => "x", b=>"y", c=>"z"]; %x<a> = "abc"; say %x<b>;'
y
Looping:
for @x.kv -> $key, $value {do something};
$ perl6 -e 'my %x = [a => "x", b=>"r", c=>"z"]; say %x; for %x.kv ->
$key, $value {say $key};'
{a => x, b => r, c => z}
a
c
b
code>
#!/usr/bin/perl6
my %y = ( 'aa'=>"AA", 'bb'=> "BB", 'cc'=>"CC", 'dd'=>"DD" );
print "loop of \%y\n";
for %y.kv -> $key, $value {
print " \%y has an key of <$key> and a value of <$value>\n"; }
print "\n";
</code>
loop of %y
%y has an key of <cc> and a value of <CC>
%y has an key of <dd> and a value of <DD>
%y has an key of <bb> and a value of <BB>
%y has an key of <aa> and a value of <AA>
Passing Hashes to Subs:
$ perl6 -e 'sub SayHash(%H) {say %H}; my %x = [aaa => "x", b=>"y",
c=>"z"];SayHash(%x);'
{aaa => x, b => y, c => z}
Destructuring Parameters (subs):
https://docs.perl6.org/type/Signature#index-entry-destructuring_arguments_%28Signature%29
sub all-dimensions(% (:length(:$x), :width(:$y), :depth(:$z))) {
$x andthen $y andthen $z andthen True }
And maybe:
sub foo(%bar (:length($)!, :width($)!)) {}
Adverbs:
Some methods are implemented as adverbs on subscripts.
:exists
The adverb :exists returns Bool::True if a key exists in the Hash. If
more than one key is supplied it returns a List of Bool.
my %h = a => 1, b => 2;
say %h<a>:exists; # OUTPUT: «True»
say %h<a b>:exists; # OUTPUT: «(True True)»
:delete
Use :delete to remove a Pair from the Hash.
my %h = a => 1;
dd %h; # Hash %h = {:a(1)}
%h<a>:delete;
dd %h; # Hash %h = {}
:p
The adverb :p returns a Pair or a List of Pair instead of just the value.
my %h = a => 1, b => 2;
say %h<a>:p; # OUTPUT: «a => 1»
say %h<a b>:p; # OUTPUT: «(a => 1 b=> 2)»
:v and :k
The adverbs :v and :k return the key or value or a list their of.
my %h = a => 1, b => 2;
say %h<a>:k; # OUTPUT: «a»
say %h<a b>:k; # OUTPUT: «(a b)»
You can also use the adverbs without knowing anything about the hash by
using empty angle brackets in which case all the keys and values will be
listed:
my %h1 = a => 1;
my %h2 = a => 1, b => 2;
say %h1<>:k; # OUTPUT: «(a)»
say %h1<>:v; # OUTPUT: «(1)»
say %h2<>:k; # OUTPUT: «(a b)»
say %h2<>:v; # OUTPUT: «(1 2)»