On 7/6/22 19:54, rir wrote:
I don't know what the free-standing '%' means;
Me either.
If this is your first time using associated
arrays (hashes), this is my Keeper on the
subject,
HTH,
-T
12/08/2019:
Perl 6 Hashes (associative arrays):
References:
https://docs.raku.org/language/subscripts#Basics
https://docs.raku.org/type/Hash#___top
https://docs.raku.org/type/Hash#:exists
https://docs.raku.org/type/Hash#method_append
A hash "associates" a Name, called a "key" to a Value, called a "value"
You assign them as follows:
# use whatever is easiest on the eyes
my %h = a => "A", b => "B"; or
my %h = ( a => "A", b => "B" ); or
my %h = [ a => "A", b => "B" ];
{a => A, b => B}
say %h.keys
(b a)
say %h.values
(B A)
Confining hashes:
Note: as of 2020-01-15, confining only works with Str and Int.
And you must confine the entire hash, not each member
my Str %h = A => "a"
{A => a}
my Int %h = A => 123
{A => 123}
my int %h = A => 123
native value types for hashes not yet implemented. Sorry.
You read them as follows:
$v = %h<b>
B
When the key is a variable, your read them as follows
$k = "a"
$v = %h{$k}
You write to them as follows:
%h<b> = "B";
When the key is a variable, your read them as follows
$k = "a"
%h{$k} = $v;
To add or delete and element, see the sections below labeled
Adding a key/value pair:
Deleting a key/value pair:
Looping through a hash:
Note: hashes DO NOT loop in the order that they were entered into
the hash
for %x.kv -> $key, $value {do something};
For example:
my %h = a => "x", b=>"r", c=>"z";
for %h.kv -> $key, $value {say "key = $key value = $value"; }
key = c value = z
key = a value = x
key = b value = r
Array's of hashes:
To access values inside and array of hashes:
my @a; my %h = a=>"A", b=>"B"; push @a, %h;
Access:
$x = @a[0]{"a"} # Note: you need the quotes
modify:
@a[0]{"b"} = "BB"
How to use arrays of hashes:
my @a;
my %h1; my %h2;
%h1 = a => 0, b => 1, c => 2;
%h2 = a => 9, b => 8, c => 7;
push @a, {%h1};
push @a, {%h2};
say @a;
[{a => 0, b => 1, c => 2} {a => 0, b => 1, c => 2}]
for @a.kv -> $i, $h { say "$i\n" ~ "$h\n"; };
# Note: the ~ is to make it easier to read
# even though $h is address as $ it is a hash
0
a 0
b 1
c 2
1
a 9
b 8
c 7
Checking for the presence of a key/value:
Warning: a Gotcha:
if using the "if" statement to check for the existence
of a key, it will return false if it does not find the key,
but it will also return false if it finds the key and its
value is a numerical zero, which "if" interprets as
a Boolean false.
Note: "exists" is called an "adverb" in this context
my %h = a => "x", b=>0, c=>"z";
if %h<d>:exists { say "exists"; } else { say "DOES NOT exist"; }
DOES NOT exist
if %h<b>:exists { say "exists"; } else { say "DOES NOT exist"; }
exists
Adding a key/value pair:
my %h = a => "x", b=>"r", c=>"z";
%h.append( 'd', "D" ) # note: you need the ''
{a => x, b => r, c => z, d => D}
Deleting a key/value pair:
Note: "delete" is called an "adverb" in this context
my %h = a => "x", b=>"r", c=>"z";
%h<b>:delete; say %h
{a => x, c => z}
Display a key/value pair (:p adverb):
my %h = a => "x", b=>"r", c=>"z";
say %h<a>:p;
a => x
say %h<a b>:p; # note: no comma between the a and the b
(a => x b => r)
Return the key and value with the :k and :v adverbs:
my %h = a => 1, b => 2;
say %h<a>:k;
a
say %h<a b>:k;
(a b)
say %h<a b>:v;
(1 2)
Empty <> return everything:
say %h<>:v;
(2 1)
say %h<>:k;
(b a)
"returns" hash on a sub declaration:
Note: "Associative" will return a hash or a map or even a pair
> sub x() returns Associative { my %h= A=>"a"; return %h}
&x
> x
{A => a}
> sub x(--> Hash) { my %h= A=>"a", B=>"b"; return %h}
&x
> x
{A => a, B => b}
>
> sub x() returns Hash { my %h= A=>"a"; return %h}
&x
> x
{A => a}