My keeper on hashes

Hi All,

My favorite variable is the associative array (hash).  I finally updated 
my keeper file on them.

If anyone is interested, here goes!

-T


Perl 6 Hashes (associative arrays):

References:
    https://docs.perl6.org/language/subscripts#Basics
    https://docs.perl6.org/type/Hash#:exists


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)

    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}
          A


    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:
        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:
        my %h = a => "x", b=>"r", c=>"z";
        if %h<c> { say "exists"; } else { say "DOES NOT exist"; }
        DOES NOT exist

        if %h<b> { say "exists"; } else { say "DOES NOT exist"; }
        exists


    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 :bv 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)
0
perl6
12/6/2019 10:39:20 AM
perl.perl6.users 1278 articles. 0 followers. Follow

7 Replies
21 Views

Similar Articles

[PageSpeed] 11

--00000000000057316605990d2772
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

El vie., 6 dic. 2019 a las 11:46, ToddAndMargo via perl6-users (<
perl6-users@perl.org>) escribi=C3=B3:

> Hi All,
>
> My favorite variable is the associative array (hash).  I finally updated
> my keeper file on them.
>
> If anyone is interested, here goes!
>
> -T
>
>
> Perl 6 Hashes (associative arrays):
>
> References:
>     https://docs.perl6.org/language/subscripts#Basics
>     https://docs.perl6.org/type/Hash#:exists
>
>
>
Please use the new URLs, those are deprecated, are no longer updated, and
might stop working without prior notice:

https://docs.raku.org/language/subscripts#Basics
https://docs.raku.org/type/Hash#:exists

Cheers

JJ

--00000000000057316605990d2772
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div dir=3D"ltr"><br></div><br><div class=3D"gmail_quote">=
<div dir=3D"ltr" class=3D"gmail_attr">El vie., 6 dic. 2019 a las 11:46, Tod=
dAndMargo via perl6-users (&lt;<a href=3D"mailto:perl6-users@perl.org">perl=
6-users@perl.org</a>&gt;) escribi=C3=B3:<br></div><blockquote class=3D"gmai=
l_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,20=
4,204);padding-left:1ex">Hi All,<br>
<br>
My favorite variable is the associative array (hash).=C2=A0 I finally updat=
ed <br>
my keeper file on them.<br>
<br>
If anyone is interested, here goes!<br>
<br>
-T<br>
<br>
<br>
Perl 6 Hashes (associative arrays):<br>
<br>
References:<br>
=C2=A0 =C2=A0 <a href=3D"https://docs.perl6.org/language/subscripts#Basics"=
 rel=3D"noreferrer" target=3D"_blank">https://docs.perl6.org/language/subsc=
ripts#Basics</a><br>
=C2=A0 =C2=A0 <a href=3D"https://docs.perl6.org/type/Hash#:exists" rel=3D"n=
oreferrer" target=3D"_blank">https://docs.perl6.org/type/Hash#:exists</a><b=
r>
<br>
<br></blockquote><div><br></div><div>Please use the new URLs, those are dep=
recated, are no longer updated, and might stop working without prior notice=
:</div></div><div><br></div><div><a href=3D"https://docs.raku.org/language/=
subscripts#Basics">https://docs.raku.org/language/subscripts#Basics</a></di=
v><div><a href=3D"https://docs.raku.org/type/Hash#:exists">https://docs.rak=
u.org/type/Hash#:exists</a></div><div><br></div><div>Cheers</div><div><br><=
/div><div>JJ<br></div></div>

--00000000000057316605990d2772--
0
jjmerelo
12/6/2019 6:24:05 PM
On 2019-12-06 10:24, JJ Merelo wrote:

> Please use the new URLs, those are deprecated, are no longer updated, 
> and might stop working without prior notice:
> 
> https://docs.raku.org/language/subscripts#Basics
> https://docs.raku.org/type/Hash#:exists
> 
> Cheers
> 
> JJ

Hi JJ,

Good catch.  Thank you!

I have refined the keeper to include your modifications
and have added how to access values inside and array
and how to add key/pairs to an existing hash.  I also
correct the :bv adverbs typo

-T


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)

    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}
          A

    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:
        my %h = a => "x", b=>"r", c=>"z";
        if %h<c> { say "exists"; } else { say "DOES NOT exist"; }
        DOES NOT exist

        if %h<b> { 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)
0
perl6
12/7/2019 12:15:34 AM
On 2019-12-06 16:15, ToddAndMargo via perl6-users wrote:
>  =C2=A0=C2=A0=C2=A0=C2=A0Checking=C2=A0for=C2=A0the=C2=A0presence=C2=A0=
of=C2=A0a=C2=A0key/value:
>  =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0my=C2=A0%h=C2=A0=3D=C2=A0a=C2=
=A0=3D>=C2=A0"x",=C2=A0b=3D>"r",=C2=A0c=3D>"z";
>  =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0if=C2=A0%h<c>=C2=A0{=C2=A0sa=
y=C2=A0"exists";=C2=A0}=C2=A0else=C2=A0{=C2=A0say=C2=A0"DOES=C2=A0NOT=C2=A0=
exist";=C2=A0}
>  =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0DOES=C2=A0NOT=C2=A0exist
>=20
>  =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0if=C2=A0%h<b>=C2=A0{=C2=A0sa=
y=C2=A0"exists";=C2=A0}=C2=A0else=C2=A0{=C2=A0say=C2=A0"DOES=C2=A0NOT=C2=A0=
exist";=C2=A0}
>  =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0exists


Oooops!  Mark caught a typo. `%h<c>` should have been ` %h<d>`

Corrected code:

     Checking for the presence of a key/value:
        my %h =3D a =3D> "x", b=3D>"r", c=3D>"z";
        if %h<d> { say "exists"; } else { say "DOES NOT exist"; }
        DOES NOT exist

        if %h<b> { say "exists"; } else { say "DOES NOT exist"; }
        exists


Thank you Mark!
-T
0
perl6
12/8/2019 2:16:41 AM
On 2019-12-07 18:30, Mark Senn wrote:
>> Corrected section
>>
>>         my %h = a => "x", b=>"r", c=>"z";
>>         if %h<d> { say "exists"; } else { say "DOES NOT exist"; }
>>         DOES NOT exist
>>
>>         if %h<b> { say "exists"; } else { say "DOES NOT exist"; }
>>         exists
> 
> Hi.
> 
> The following code prints DOES NOT exist twice.
> 
> my %h = a => "x", b=>0, c=>"z";
> if %h<d> { say "exists"; } else { say "DOES NOT exist"; }
> 
> if %h<b> { say "exists"; } else { say "DOES NOT exist"; }
> 
> 
> I changed the b=>"r" to b=>0.  I think it is evaluating
> the b element as a boolean---I think the exists adverb
> needs to be used to check for existence.
> 
> -m
> 

Hi Mark,

Oh bugger!


This is what I get:

$ perl6
To exit type 'exit' or '^D'
 > my %h = a => "x", b=>"r", c=>"z";
{a => x, b => r, c => z}

 > if %h<d> { say "exists"; } else { say "DOES NOT exist"; }
DOES NOT exist

 > if %h<b> { say "exists"; } else { say "DOES NOT exist"; }
exists


Would someone else please run my code and see if
they can reproduce Mark's error?

Many thanks,
-T
0
perl6
12/8/2019 5:34:26 AM
On Sat, Dec 7, 2019 at 9:36 PM ToddAndMargo via perl6-users
<perl6-users@perl.org> wrote:
>
> On 2019-12-07 18:30, Mark Senn wrote:
> >> Corrected section
> >>
> >>         my %h = a => "x", b=>"r", c=>"z";
> >>         if %h<d> { say "exists"; } else { say "DOES NOT exist"; }
> >>         DOES NOT exist
> >>
> >>         if %h<b> { say "exists"; } else { say "DOES NOT exist"; }
> >>         exists
> >
> > Hi.
> >
> > The following code prints DOES NOT exist twice.
> >
> > my %h = a => "x", b=>0, c=>"z";
> > if %h<d> { say "exists"; } else { say "DOES NOT exist"; }
> >
> > if %h<b> { say "exists"; } else { say "DOES NOT exist"; }
> >
> >
> > I changed the b=>"r" to b=>0.  I think it is evaluating
> > the b element as a boolean---I think the exists adverb
> > needs to be used to check for existence.
> >
> > -m
> >
>
> Hi Mark,
>
> Oh bugger!
>
>
> This is what I get:
>
> $ perl6
> To exit type 'exit' or '^D'
>  > my %h = a => "x", b=>"r", c=>"z";
> {a => x, b => r, c => z}
>
>  > if %h<d> { say "exists"; } else { say "DOES NOT exist"; }
> DOES NOT exist
>
>  > if %h<b> { say "exists"; } else { say "DOES NOT exist"; }
> exists
>
>
> Would someone else please run my code and see if
> they can reproduce Mark's error?
>
> Many thanks,
> -T

Mark is correct. Changing values to 1 or 0 results in if/else
evaluating as a True/False:

mbook:~ homedir$ perl6
To exit type 'exit' or '^D'
> my %i = a => 1, b=> 0, c=> 1;
{a => 1, b => 0, c => 1}
> if %i<a> { say "exists"; } else { say "DOES NOT exist"; }
exists
> if %i<b> { say "exists"; } else { say "DOES NOT exist"; }
DOES NOT exist
> if %i<d> { say "exists"; } else { say "DOES NOT exist"; }
DOES NOT exist
>
> $*VM
moar (2019.07.1)
>

HTH, Bill.
0
perl6
12/8/2019 6:43:49 AM
On 2019-12-07 22:43, William Michels via perl6-users wrote:
> On Sat, Dec 7, 2019 at 9:36 PM ToddAndMargo via perl6-users
> <perl6-users@perl.org> wrote:
>>
>> On 2019-12-07 18:30, Mark Senn wrote:
>>>> Corrected section
>>>>
>>>>          my %h = a => "x", b=>"r", c=>"z";
>>>>          if %h<d> { say "exists"; } else { say "DOES NOT exist"; }
>>>>          DOES NOT exist
>>>>
>>>>          if %h<b> { say "exists"; } else { say "DOES NOT exist"; }
>>>>          exists
>>>
>>> Hi.
>>>
>>> The following code prints DOES NOT exist twice.
>>>
>>> my %h = a => "x", b=>0, c=>"z";
>>> if %h<d> { say "exists"; } else { say "DOES NOT exist"; }
>>>
>>> if %h<b> { say "exists"; } else { say "DOES NOT exist"; }
>>>
>>>
>>> I changed the b=>"r" to b=>0.  I think it is evaluating
>>> the b element as a boolean---I think the exists adverb
>>> needs to be used to check for existence.
>>>
>>> -m
>>>
>>
>> Hi Mark,
>>
>> Oh bugger!
>>
>>
>> This is what I get:
>>
>> $ perl6
>> To exit type 'exit' or '^D'
>>   > my %h = a => "x", b=>"r", c=>"z";
>> {a => x, b => r, c => z}
>>
>>   > if %h<d> { say "exists"; } else { say "DOES NOT exist"; }
>> DOES NOT exist
>>
>>   > if %h<b> { say "exists"; } else { say "DOES NOT exist"; }
>> exists
>>
>>
>> Would someone else please run my code and see if
>> they can reproduce Mark's error?
>>
>> Many thanks,
>> -T
> 
> Mark is correct. Changing values to 1 or 0 results in if/else
> evaluating as a True/False:
> 
> mbook:~ homedir$ perl6
> To exit type 'exit' or '^D'
>> my %i = a => 1, b=> 0, c=> 1;
> {a => 1, b => 0, c => 1}
>> if %i<a> { say "exists"; } else { say "DOES NOT exist"; }
> exists
>> if %i<b> { say "exists"; } else { say "DOES NOT exist"; }
> DOES NOT exist
>> if %i<d> { say "exists"; } else { say "DOES NOT exist"; }
> DOES NOT exist
>>
>> $*VM
> moar (2019.07.1)
>>
> 
> HTH, Bill.
> 

Thank you.

Back to the drawing board.  I think there is an adverb for this.
0
perl6
12/8/2019 9:25:45 AM
On 2019-12-06 02:39, ToddAndMargo via perl6-users wrote:
> Hi All,
>=20
> My favorite variable is the associative array (hash).=C2=A0 I finally u=
pdated=20
> my keeper file on them.
>=20
> If anyone is interested, here goes!
>=20
> -T
>=20

Hi All,

Okay, here it is again with the booboo Mark pointed
to me corrected.

Please feel free to criticize anything you find wrong,
there may be other booboo too.

-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 =3D   a =3D> "A", b =3D> "B";      or
       my %h =3D ( a =3D> "A", b =3D> "B" );    or
       my %h =3D [ a =3D> "A", b =3D> "B" ];
       {a =3D> A, b =3D> B}

       say %h.keys
       (b a)

       say %h.values
       (B A)

    You read them as follows:
       $v =3D %h<b>
       B

       When the key is a variable, your read them as follows
          $k =3D "a"
          $v =3D %h{$k}
          A

    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=20
the hash

        for @x.kv -> $key, $value {do something};

        For example:

           my %h =3D a =3D> "x", b=3D>"r", c=3D>"z";
           for %h.kv ->  $key, $value {say "key =3D $key  value =3D $valu=
e"; }
           key =3D c  value =3D z
           key =3D a  value =3D x
           key =3D b  value =3D r

    Array's of hashes:
        To access values inside and array of hashes:
              my @a; my %h =3D a=3D>"A", b=3D>"B"; push @a, %h;
              Access:
                  $x =3D @a[0]{"a"}    # Note: you need the quotes

              modify:
                  @a[0]{"b"} =3D "BB"

        How to use arrays of hashes:
           my @a;
           my %h1; my %h2;

           %h1 =3D a =3D> 0, b =3D> 1, c =3D> 2;
           %h2 =3D a =3D> 9, b =3D> 8, c =3D> 7;

           push @a, {%h1};
          push @a, {%h2};

           say @a;
           [{a =3D> 0, b =3D> 1, c =3D> 2} {a =3D> 0, b =3D> 1, c =3D> 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 =3D a =3D> "x", b=3D>0, c=3D>"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 =3D a =3D> "x", b=3D>"r", c=3D>"z";
        %h.append( 'd',  "D" )               # note: you need the ''
        {a =3D> x, b =3D> r, c =3D> z, d =3D> D}


    Deleting a key/value pair:
    Note: "delete" is called an "adverb" in this context
        my %h =3D a =3D> "x", b=3D>"r", c=3D>"z";
        %h<b>:delete; say %h
        {a =3D> x, c =3D> z}


    Display a key/value pair  (:p adverb):
        my %h =3D a =3D> "x", b=3D>"r", c=3D>"z";
        say %h<a>:p;
        a =3D> x
        say %h<a b>:p;   # note: no comma between the a and the b
        (a =3D> x b =3D> r)


    Return the key and value with the :k and :v adverbs:
        my %h =3D a =3D> 1, b =3D> 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)
0
perl6
12/8/2019 10:31:48 AM
Reply: