Re: Confused after consuming hashish

2022-07-07 Thread rir


Hi all,

On Wed, Jul 06, 2022 at 08:44:02PM -0700, William Michels via perl6-users wrote:

> I'm assuming the `%` is the anonymous state variable (associative)?

> https://docs.raku.org/language/variables#The_%_variable

Thanks, Bill, that is a significant bit (Ha, I'll keep that to punish
everyone).  I have managed to both miss and not retain that.  Now it makes
some sense.

Now, I can see that

%h=%=%(:a(1));   #  «{a => 1}␤»

easily gets parsed to

%h = %=  %(:a(1));   #  «{a => 1}␤»

but not how the "modulus assignment" is rejected for the "assign to anonymous 
state var".
That is despite that only the one interpretation will work with the type given 
here.

Rob



Re: Confused after consuming hashish

2022-07-07 Thread ToddAndMargo via perl6-users

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

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

   You write to them as follows:
   %h = "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:exists { say "exists"; } else { say "DOES NOT exist"; }
  DOES NOT exist

  if %h: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:delete; say %h
   {a => x, c => z}


   Display a key/value pair  (:p adverb):
   my %h = a => "x", b=>"r", c=>"z";
   say %h:p;
   a => x
   say %h: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:k;
   a

   say %h:k;
  (a b)

  say %h: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
{A => a}

> sub x(--> Hash) { my %h= A=>"a", B=>"b"; return %h}

> x
{A => a, B => b}
>

> sub x() returns Hash { my %h= A=>"a"; return %h}

> x
{A => a}






Re: Confused after consuming hashish

2022-07-06 Thread William Michels via perl6-users
I'm assuming the `%` is the anonymous state variable (associative)?

https://docs.raku.org/language/variables#The_%_variable

Best guess for now, Bill.



On Wed, Jul 6, 2022 at 8:10 PM rir  wrote:
>
> Hi,
> Sorry, my previous message got away from me a little to soon.
> I'll stand by it without the last partial sentence,
> and, more important, the addition of a greeting and
> some good will.
>
> Thanks,
> Rob
>
>
> On Wed, Jul 06, 2022 at 10:54:11PM -0400, rir wrote:
> >
> > This is sharing a 'What?!!' moment and some aftermath with the
> > hope of some reduction of ignorance.
> >
> > This is from some code I found:
> >
> > say my %h = % = %(:a(1));   # OUTPUT: «{a => 1}␤»
> >
> > All whitespace after the identifier is optional--not changing the
> > assignment.
> >
> > Reduced further:
> >
> > say % = %(:a(1));  # OUTPUT: «{a => 1}␤»
> >
> > I don't know what the free-standing '%' means; my guess is that
> > it accepts the result of the assign-op as a term.  So it is the
> > same as the last '%'.
> >
> > Is that correct?  Correct as Raku code and as what is happening.
> >
> > Why, in the ' %= ' variant, is neither of these failing?
> > Is it not ambiguous with the '%=' operator?
> >
> > And I found that '%=' is also indexed in the docs as a twigil,
> > but it is not in the docs.  Is i
> >
> >


Re: Confused after consuming hashish

2022-07-06 Thread rir
Hi,
Sorry, my previous message got away from me a little to soon.
I'll stand by it without the last partial sentence,
and, more important, the addition of a greeting and
some good will.

Thanks,
Rob


On Wed, Jul 06, 2022 at 10:54:11PM -0400, rir wrote:
> 
> This is sharing a 'What?!!' moment and some aftermath with the
> hope of some reduction of ignorance.
> 
> This is from some code I found:
> 
> say my %h = % = %(:a(1));   # OUTPUT: «{a => 1}␤»
> 
> All whitespace after the identifier is optional--not changing the
> assignment.
> 
> Reduced further:
> 
> say % = %(:a(1));  # OUTPUT: «{a => 1}␤»
> 
> I don't know what the free-standing '%' means; my guess is that
> it accepts the result of the assign-op as a term.  So it is the
> same as the last '%'.
> 
> Is that correct?  Correct as Raku code and as what is happening.
> 
> Why, in the ' %= ' variant, is neither of these failing?
> Is it not ambiguous with the '%=' operator?
> 
> And I found that '%=' is also indexed in the docs as a twigil,
> but it is not in the docs.  Is i
> 
>