Re: stashing an array in a hash and yanking it back out

2020-03-13 Thread Vadim Belman

There is no mystery whatsoever.

Consider the following:

my %h = "a", 1; # {a => 1}

Then consider this:

say *, *; # **


and also:

say *.VAR.WHAT; # (Whatever)

Taking into account that => has tighter precedence than , what you get in:

my %h = *, a => [1,2,3];

is actually the following data structure:

%( Whatever => Pair )

Regarding your use of postcircumfix [ ] on the data, you use it on Pair.

Best regards,
Vadim Belman

> On Mar 13, 2020, at 11:52 AM, Andy Bach  wrote:
> 
> > my  %stash = monsters => @monsters, rocks => @rocks
> {monsters => [godzilla grendel wormface blob fingfangfoom tingler], rocks => 
> [marble sandstone granite chert pumice limestone]}
> > my @more_rocks = << marble sandstone granite chert pumice limestone >>
> [marble sandstone granite chert pumice limestone]
> > my  %stash = *, morerocks => @rocks
> {* => morerocks => [marble sandstone granite chert pumice limestone]}
> > say %stash{*}
> (morerocks => [marble sandstone granite chert pumice limestone])
> 
> So, I'm guessing the display
> {* => morerocks => [marble sandstone granite chert pumice limestone]}
> 
> really means something like
> {* => (morerocks => [marble sandstone granite chert pumice limestone])}
> 
> maybe?
> > say @(%stash{*})
> (morerocks => [marble sandstone granite chert pumice limestone])
> > say @(%stash{*}).[0]
> morerocks => [marble sandstone granite chert pumice limestone]
> > say @(%stash{*}).[1]
> Nil
> > say @(%stash{*}).[0].{morerocks}
> ===SORRY!=== Error while compiling:
> Undeclared routine:
> morerocks used at line 1
> 
> > say @(%stash{*}).[0].[0]
> morerocks => [marble sandstone granite chert pumice limestone]
> > say @(%stash{*}).[0].[1]
> Index out of range. Is: 1, should be in 0..0
>   in block  at  line 1
> 
> > say @(%stash{*}).[0].[0].perl
> :morerocks(["marble", "sandstone", "granite", "chert", "pumice", "limestone"])
> > say @(%stash{*}).[0].perl
> :morerocks(["marble", "sandstone", "granite", "chert", "pumice", "limestone"])
> 
> 
> I dunno.
> 
> From: William Michels via perl6-users  >
> Sent: Thursday, March 12, 2020 5:44 PM
> To: perl6-users mailto:perl6-users@perl.org>>
> Cc: Joseph Brenner mailto:doom...@gmail.com>>; Timo 
> Paulssen mailto:t...@wakelift.de>>; yary 
> mailto:not@gmail.com>>
> Subject: Re: stashing an array in a hash and yanking it back out
>  
> Thanks yary! The code you posted works perfectly.
> 
> Okay, one last question. I tried to use the 'DRY' principle to add
> things to a hash. However, (thinking that a 'whatever star' might
> reduce typing), I came up with an odd "ternary" structure. Can anyone
> explain the last line of code, below?
> 
> mbook:~ homedir$ perl6
> To exit type 'exit' or '^D'
> > my @monsters = << godzilla grendel wormface blob fingfangfoom tingler >>;
> [godzilla grendel wormface blob fingfangfoom tingler]
> > my @rocks = << marble sandstone granite chert pumice limestone >>
> [marble sandstone granite chert pumice limestone]
> > my  %stash = monsters => @monsters
> {monsters => [godzilla grendel wormface blob fingfangfoom tingler]}
> > my %stash = *, rocks => @rocks;
> {* => rocks => [marble sandstone granite chert pumice limestone]}
> 
> Thanks, Bill.
> 
> 
> On Wed, Mar 11, 2020 at 9:06 PM yary  > wrote:
> >
> > The fat-arrow example makes sense, what this says
> > %stash = rocks => @rocks
> > is "replace %stash in its entirety with key rocks gets value @rocks"
> > anything that used to be in %stash doesn't matter because this assignment 
> > (left side) is the entirety of %stash
> >
> > what this says
> > %stash{'rocks'} = @rocks
> > is "replace the slot 'rocks' in %stash with @rocks"
> > This assignment only is for the 'rocks' element of %stash so the other 
> > elements remain unchanged.
> >
> > Extending the examples, first 3 lines are unchanged from before
> >
> > > my @monsters = << godzilla grendel wormface blob fingfangfoom tingler >>;
> > [godzilla grendel wormface blob fingfangfoom tingler]
> > > my @rocks = << marble sandstone granite chert pumice limestone >>
> > [marble sandstone granite chert pumice limestone]
> > > my  %stash = monsters => @monsters
> > {monsters => [godzilla grendel wormface blob fingfangfoom tingler]}
> >
> > > %stash = %stash, rocks => @rocks
> > {monsters => [godzilla grendel wormface blob fingfangfoom tingler], rocks 
> > => [marble sandstone granite chert pumice limestone]}
> > > my %together = monsters => @monsters, rocks => @rocks
> > {monsters => [godzilla grendel wormface blob fingfangfoom tingler], rocks 
> > => [marble sandstone granite chert pumice limestone]}
> >
> >
> > -y
> >
> >
> > On Tue, Mar 10, 2020 at 1:12 PM William Michels via perl6-users 
> > mailto:perl6-users@perl.org>> wrote:
> >>
> >> Hi Joe,
> >>
> >> So I had a chance to play with hashes further, and I noticed something
> >> that you might be interested in. It seems that 'bare' declaration of a
> >> hash with a "my" lexical scope enables you to stash away 

Re: stashing an array in a hash and yanking it back out

2020-03-13 Thread Andy Bach
> my  %stash = monsters => @monsters, rocks => @rocks
{monsters => [godzilla grendel wormface blob fingfangfoom tingler], rocks => 
[marble sandstone granite chert pumice limestone]}
> my @more_rocks = << marble sandstone granite chert pumice limestone >>
[marble sandstone granite chert pumice limestone]
> my  %stash = *, morerocks => @rocks
{* => morerocks => [marble sandstone granite chert pumice limestone]}
> say %stash{*}
(morerocks => [marble sandstone granite chert pumice limestone])

So, I'm guessing the display
{* => morerocks => [marble sandstone granite chert pumice limestone]}

really means something like
{* => (morerocks => [marble sandstone granite chert pumice limestone])}

maybe?
> say @(%stash{*})
(morerocks => [marble sandstone granite chert pumice limestone])
> say @(%stash{*}).[0]
morerocks => [marble sandstone granite chert pumice limestone]
> say @(%stash{*}).[1]
Nil
> say @(%stash{*}).[0].{morerocks}
===SORRY!=== Error while compiling:
Undeclared routine:
morerocks used at line 1

> say @(%stash{*}).[0].[0]
morerocks => [marble sandstone granite chert pumice limestone]
> say @(%stash{*}).[0].[1]
Index out of range. Is: 1, should be in 0..0
  in block  at  line 1

> say @(%stash{*}).[0].[0].perl
:morerocks(["marble", "sandstone", "granite", "chert", "pumice", "limestone"])
> say @(%stash{*}).[0].perl
:morerocks(["marble", "sandstone", "granite", "chert", "pumice", "limestone"])


I dunno.


From: William Michels via perl6-users 
Sent: Thursday, March 12, 2020 5:44 PM
To: perl6-users 
Cc: Joseph Brenner ; Timo Paulssen ; yary 

Subject: Re: stashing an array in a hash and yanking it back out

Thanks yary! The code you posted works perfectly.

Okay, one last question. I tried to use the 'DRY' principle to add
things to a hash. However, (thinking that a 'whatever star' might
reduce typing), I came up with an odd "ternary" structure. Can anyone
explain the last line of code, below?

mbook:~ homedir$ perl6
To exit type 'exit' or '^D'
> my @monsters = << godzilla grendel wormface blob fingfangfoom tingler >>;
[godzilla grendel wormface blob fingfangfoom tingler]
> my @rocks = << marble sandstone granite chert pumice limestone >>
[marble sandstone granite chert pumice limestone]
> my  %stash = monsters => @monsters
{monsters => [godzilla grendel wormface blob fingfangfoom tingler]}
> my %stash = *, rocks => @rocks;
{* => rocks => [marble sandstone granite chert pumice limestone]}

Thanks, Bill.


On Wed, Mar 11, 2020 at 9:06 PM yary  wrote:
>
> The fat-arrow example makes sense, what this says
> %stash = rocks => @rocks
> is "replace %stash in its entirety with key rocks gets value @rocks"
> anything that used to be in %stash doesn't matter because this assignment 
> (left side) is the entirety of %stash
>
> what this says
> %stash{'rocks'} = @rocks
> is "replace the slot 'rocks' in %stash with @rocks"
> This assignment only is for the 'rocks' element of %stash so the other 
> elements remain unchanged.
>
> Extending the examples, first 3 lines are unchanged from before
>
> > my @monsters = << godzilla grendel wormface blob fingfangfoom tingler >>;
> [godzilla grendel wormface blob fingfangfoom tingler]
> > my @rocks = << marble sandstone granite chert pumice limestone >>
> [marble sandstone granite chert pumice limestone]
> > my  %stash = monsters => @monsters
> {monsters => [godzilla grendel wormface blob fingfangfoom tingler]}
>
> > %stash = %stash, rocks => @rocks
> {monsters => [godzilla grendel wormface blob fingfangfoom tingler], rocks => 
> [marble sandstone granite chert pumice limestone]}
> > my %together = monsters => @monsters, rocks => @rocks
> {monsters => [godzilla grendel wormface blob fingfangfoom tingler], rocks => 
> [marble sandstone granite chert pumice limestone]}
>
>
> -y
>
>
> On Tue, Mar 10, 2020 at 1:12 PM William Michels via perl6-users 
>  wrote:
>>
>> Hi Joe,
>>
>> So I had a chance to play with hashes further, and I noticed something
>> that you might be interested in. It seems that 'bare' declaration of a
>> hash with a "my" lexical scope enables you to stash away multiple
>> 'hash' elements at the top level using a 'curly brace' syntax. However
>> using the 'fat arrow' syntax will overwrite any previously stashed
>> 'top level' hash elements.
>>
>> Hopefully the REPL code below illustrates. First, 'curly brace' syntax:
>>
>> mbook:~ homedir$ perl6
>> To exit type 'exit' or '^D'
>> > my @monsters = << godzilla grendel wormface blob fingfangfoom tingler >>;
>> [godzilla grendel wormface blob fingfangfoom tingler]
>> > my @rocks = << marble sandstone granite chert pumice limestone >>
>> [marble sandstone granite chert pumice limestone]
>> > my %stash
>> {}
>> > %stash{'monsters'} = @monsters
>> [godzilla grendel wormface blob fingfangfoom tingler]
>> > say %stash
>> {monsters => [godzilla grendel wormface blob fingfangfoom tingler]}
>> > %stash{'rocks'} = @rocks
>> [marble sandstone granite chert pumice limestone]
>> > say