Sorry, I thought I was replying on list...

I was trying to remind that what William Michels was asking about was
a way to assign an array to a hash field named after the array, but
without manually typing the name twice.

These both work, but aren't what he was asking about:

  %stash{'greek'}    = @greek;

   %stash.append: (:greek(@greek));


---------- Forwarded message ----------
From: Joseph Brenner <doom...@gmail.com>
Date: Mon, 16 Mar 2020 14:10:01 -0700
Subject: Re: stashing an array in a hash and yanking it back out
To: Vadim Belman <vr...@lflat.org>

Vadim Belman<vr...@lflat.org> wrote:

> So, you basically needed:
>
> my %h = :a(1); %h.append: (:b(2));
>
> Am I correct?
Well, not exactly.

The kind of thing I was doing is stashing the contents of an
array in a hash field with the same name:

  my @greek = << alpha beta gamma >>;
  %stash{'greek'}    = @greek;

So doing that with append would be like this:

  %stash.append: (:greek(@greek));

William Michels was wondering if there was a way to avoid
repeating the name twice, speculating that there might be
some way to do that with a 'whateva' *.

My personal take would be that even if there's a way to do that
that I'm missing, it's probably a little too clever.  You can
over do things like "DRY"...  It's pretty clear at a glance what
a line like this is trying to do:

  %stash{'greek'} = @greek;



On 3/16/20, Vadim Belman <vr...@lflat.org> wrote:
> So, you basically needed:
>
> my %h = :a(1); %h.append: (:b(2));
>
> Am I correct?
>
> Best regards,
> Vadim Belman
>
>> On Mar 16, 2020, at 11:13 AM, Andy Bach <andy_b...@wiwb.uscourts.gov>
>> wrote:
>>
>> > Due to rather weird formatting in your message I hardly can understand
>> > what is it all about.
>> No worries,  I'm often quite befuddling, esp. when I think I'm being
>> extra-clear about things.  I was trying to include enough of the previous
>> thread, in context, to make any replies more coherent.  Then again, I'm
>> using Outlook and I never really know what it's going to do to my outgoing
>> msgs.
>>
>> It wasn't my idea to use the "*", Joseph, the original poster was thinking
>> they could save rekeying (or something) the current contents of the hash
>> by using the "*" in the assignment list, when adding a new pair without
>> specifying the key on the LHS. Whoops, it was William who tried the
>> "whatever star" problem.
>>
>>>>  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]}
>>
>> From: Vadim Belman <vr...@lflat.org>
>> Sent: Sunday, March 15, 2020 5:18 PM
>> To: Andy Bach <andy_b...@wiwb.uscourts.gov>
>> Cc: William Michels via perl6-users <perl6-users@perl.org>; Joseph Brenner
>> <doom...@gmail.com>; Timo Paulssen <t...@wakelift.de>; yary
>> <not....@gmail.com>
>> Subject: Re: stashing an array in a hash and yanking it back out
>>
>> Due to rather weird formatting in your message I hardly can understand
>> what is it all about. But before you can find an answer on how to get the
>> array out of the hash, try answering the following question: why do you
>> use bare asterisk in the hash initialization? What is its purpose over
>> there? To me this looks like the key to all your issues.
>>
>> With regard to Pair type object, there is a little magic about it:
>>
>> my $p = a => [1,2]; say $p<a>;
>>
>> Or, in your case that'd be something like:
>>
>> my %h = *, a => <b c>; say %h<*><a>;
>>
>> Though I'd still insist on reconsidering how you do things.
>>
>> Best regards,
>> Vadim Belman
>>
>>> On Mar 15, 2020, at 5:41 PM, Andy Bach <andy_b...@wiwb.uscourts.gov
>>> <mailto:andy_b...@wiwb.uscourts.gov>> wrote:
>>>
>>> >> really means something like
>>> {* => (morerocks => [marble sandstone granite chert pumice limestone])}
>>>
>>> > 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 )
>>>
>>> That's sort of what I said, or, at least, saw.
>>> > Regarding your use of postcircumfix [ ] on the data, you use it on
>>> > Pair.
>>>
>>> Not quite sure what this means, but is that how you'd get the [<list of
>>> rocks>] array from %stash? I could get the pair back, but not the "inner"
>>> array of the pair's 2nd partner, so to speak:
>>> >> 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]
>>>
>>>
>>> a
>>>
>>> From: Vadim Belman <vr...@lflat.org <mailto:vr...@lflat.org>>
>>> Sent: Friday, March 13, 2020 12:50 PM
>>> To: Andy Bach <andy_b...@wiwb.uscourts.gov
>>> <mailto:andy_b...@wiwb.uscourts.gov>>
>>> Cc: William Michels via perl6-users <perl6-users@perl.org
>>> <mailto:perl6-users@perl.org>>; Joseph Brenner <doom...@gmail.com
>>> <mailto:doom...@gmail.com>>; Timo Paulssen <t...@wakelift.de
>>> <mailto:t...@wakelift.de>>; yary <not....@gmail.com
>>> <mailto:not....@gmail.com>>
>>> Subject: Re: stashing an array in a hash and yanking it back out
>>>
>>>
>>> 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 <andy_b...@wiwb.uscourts.gov
>>>> <mailto:andy_b...@wiwb.uscourts.gov>> 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 <unit> at <unknown file> 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 <perl6-users@perl.org
>>>> <mailto:perl6-users@perl.org>>
>>>> Sent: Thursday, March 12, 2020 5:44 PM
>>>> To: perl6-users <perl6-users@perl.org <mailto:perl6-users@perl.org>>
>>>> Cc: Joseph Brenner <doom...@gmail.com <mailto:doom...@gmail.com>>; Timo
>>>> Paulssen <t...@wakelift.de <mailto:t...@wakelift.de>>; yary
>>>> <not....@gmail.com <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 <not....@gmail.com
>>>> <mailto:not....@gmail.com>> 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
>>>> > <perl6-users@perl.org <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 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 %stash
>>>> >> {monsters => [godzilla grendel wormface blob fingfangfoom tingler],
>>>> >> rocks => [marble sandstone granite chert pumice limestone]}
>>>> >> > exit
>>>> >> mbook:~ homedir$
>>>> >>
>>>> >> [and now try 'fat arrow' 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
>>>> >> {monsters => [godzilla grendel wormface blob fingfangfoom tingler]}
>>>> >> > %stash = rocks => @rocks
>>>> >> {rocks => [marble sandstone granite chert pumice limestone]}
>>>> >> > say %stash
>>>> >> {rocks => [marble sandstone granite chert pumice limestone]}
>>>> >> > say %stash<monsters>
>>>> >> (Any)
>>>> >> > exit
>>>> >> mbook:~ homedir$ perl6 -v
>>>> >> This is Rakudo version 2019.07.1 built on MoarVM version 2019.07.1
>>>> >> implementing Perl 6.d.
>>>> >>
>>>> >> HTH, Bill.
>>>> >>
>>>> >>
>>>> >>
>>>> >> On Thu, Mar 5, 2020 at 6:10 PM Joseph Brenner <doom...@gmail.com
>>>> >> <mailto:doom...@gmail.com>> wrote:
>>>> >> >
>>>> >> > William Michels <w...@caa.columbia.edu
>>>> >> > <mailto:w...@caa.columbia.edu>> wrote:
>>>> >> >
>>>> >> > > Yes, since I was working in the REPL, I tried compacting Joe's
>>>> >> > > code by
>>>> >> > > eliminating the "my %stash" line at the top, and adding "my" to
>>>> >> > > the third
>>>> >> > > line.
>>>> >> >
>>>> >> > I noticed the additional "my" in there, but I wouldn't have been
>>>> >> > able
>>>> >> > to tell you why it was behaving like it was...
>>>> >> >
>>>> >> > On the plus side, I see that if you tried to do that in a script,
>>>> >> > it
>>>> >> > would warn you:
>>>> >> >
>>>> >> >     Potential difficulties:
>>>> >> >        Redeclaration of symbol '%stash'
>
>

Reply via email to