D:\perl6.scripts\myown>perl6 -v
This is Rakudo Star version 2018.01 built on MoarVM version 2018.01
implementing Perl 6.c.
####
use v6;
# create 3 hashes, showing mixed inits that can be used in perl6 :
my %hsh0 = ( "Name" => "George H. W. Bush", "Function" => , 'Time' => <2001-2009>);
my %hsh1 = ( "Name" => "Bill Clinton", "Function" => , 'Time' => <1993-2001>) ;
my %hsh2 = ( "Name" , "Barack Obama", "Function" , , 'Time' , <2009-2017> ) ;
# Subscript Adverbs : :exists :k :v :p :delete
#if ( %hsh1:exists) { # Use this when keys have No Spaces !!
my $key = 'Name' ;
if ( %hsh1{$key}:k) { # Can I use %hsh1{Name}:exists : Yes works!
say "Found Name element in %hsh1";
}
# there is an other use for <> construct : , but {} also works!!
say %hsh1{}:v.perl; #Show all values
say %hsh1.pairs.perl;
# push the several hashes onto the array : @arr
my @arr ;
# Please notice the : after the push
@arr.push: { %hsh0 } ;
@arr.push: { %hsh1 } ;
@arr.push: { %hsh2 } ;
# show some entries. using serveral formats that perl6 has :
say '@arr.[0].{"Name"} = ' ~ @arr.[0].{'Name'};
say '@arr.[1]. = ' ~ @arr.[1]. ;
say @arr.end.fmt('%+4d') ; # How many items ( hashes ) are in the array.
# dump @arr
dd @arr;
# try looping over the @arr , and detect the keys stored in the hash. It's more simple than I thought ( after some experimenting )
for 0 .. @arr.end -> $idx {
say "@arr idx : $idx";
my %x = @arr[$idx];
for %x.kv -> $key, $value {
printf "%10.10s : %-20.20s\n" , $key, $value; # please notice printf NOT using () ;
}
}
##
##
D:\perl6.scripts\myown>perl6 array_hash_3.p6
Found Name element in %hsh1
($("president", "of", "USA"), "1993-2001", "Bill Clinton")
(:Function($("president", "of", "USA")), :Time("1993-2001"), :Name("Bill Clinton")).Seq
@arr.[0].{"Name"} = George H. W. Bush
@arr.[1]. = Bill Clinton
+2
Array @arr = [{:Function($("president", "of", "USA")), :Name("George H. W. Bush"), :Time("2001-2009")}, {:Function($("president", "of", "USA")), :Name("Bill Clinton"), :Time("1993-2001")}, {:Function($("President", "of", "USA")), :Name("Barack Obama"), :Time("2009-2017")}]
@arr idx : 0
Function : president of USA
Time : 2001-2009
Name : George H. W. Bush
@arr idx : 1
Function : president of USA
Time : 1993-2001
Name : Bill Clinton
@arr idx : 2
Function : President of USA
Time : 2009-2017
Name : Barack Obama