Hi all,
Andy Bach contacted me off list, to say my postscript comment about
"prepending" (versus "appending") to a hash object was nonsensical. I
agree.
Hashes in Raku/Perl6 are guaranteed to be unordered upon return. So
once they are declared, it doesn't make sense to think of them as a
(double-
Okay, here's another (simpler?) approach using the ",= " postfix operator:
mbook:~ homedir$ perl6
To exit type 'exit' or '^D'
> my %stash;
{}
> my @monsters = << godzilla grendel wormface blob >>;
[godzilla grendel wormface blob]
> my @rabbits = << bugs peter easter >>;
[bugs peter easter]
> %stas
Yes you're right: I could've sworn I tried that in the repl a minute
ago and it worked, but actually it's a no-op and appends nothing to
the hash.
This is okay, doing it the other way (without the inner parens around
the colonpair) is not:
ny %stash;
my @monsters = << godzilla grendel wormface bl
Joseph, you've got yourself into a trap I fell into yesterday. %stash.append(
:@stuff ) syntax is about calling append method with a named parameter stuff
whereas append works with positionals only. So, your case should be written:
%stash.append( (:@stuff) );
Which is apparently more cumbersom
zilla grendel wormface blob fingfangfoom tingler], rocks =>
> [marble sandstone granite chert pumice limestone]}
>
> Though I've no idea what those colons are/are not doing. And we can get to
> those "inner" array elements via
>> say %stash[1]
> sandstone
>
&g
t;>>>> > my %stash = monsters => @monsters
>>>>> {monsters => [godzilla grendel wormface blob fingfangfoom tingler]}
>>>>> > %stash.append: (rocks => @rocks);
>>>>> {monsters => [godzilla grendel wormface blob fingfangfoom ti
Vadim seems to have provided the definitive answer:
Ok, clear enough. This is as simple as:
> %stash.append: (:@greek);
rendel wormface blob fingfangfoom tingler]}
>>>> > %stash.append: (rocks => @rocks);
>>>> {monsters => [godzilla grendel wormface blob fingfangfoom tingler], rocks
>>>> => [marble sandstone granite chert pumice limestone]}
>>>>
> Though I'
those colons are/are not doing. And we can get to
those "inner" array elements via
> say %stash[1]
sandstone
From: Vadim Belman
Sent: Friday, March 13, 2020 12:50 PM
To: Andy Bach
Cc: William Michels via perl6-users ; Joseph Brenner
; Timo Paulssen ; yary
Subject: Re: stashing
ne 1
>
> >> say @(%stash{*}).[0].[0]
> morerocks => [marble sandstone granite chert pumice limestone]
>
>
> a
>
> From: Vadim Belman
> Sent: Friday, March 13, 2020 12:50 PM
> To: Andy Bach
> Cc: William Michels via perl6-users ; Joseph Brenner
&
", "chert", "pumice", "limestone"])
> say @(%stash{*}).[0].perl
:morerocks(["marble", "sandstone", "granite", "chert", "pumice", "limestone"])
I dunno.
____________________
From: William Mich
uot;granite", "chert", "pumice", "limestone"])
> > say @(%stash{*}).[0].perl
> :morerocks(["marble", "sandstone", "granite", "chert", "pumice", "limestone"])
>
>
> I dunno.
>
> From: Will
[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: Josep
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:
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
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 '
William Michels 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...
Timo Paulssen wrote:
> The reason for that is that `my %foo{Bar}` is syntax for restricting
> the keys of the hash to a specific type (by default it's `Str(Any)`)
Hm.. That's certainly useful, I hadn't even gotten to wondering how to
do that yet.
> and giving a string instance ("monsters" in
Hi Timo and thank you for the note.
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 figured since Joe's code looked like a closure (curly brackets and all),
it wouldn't be an issue.
But the two
Hi William,
The line that has the important difference is where you put a `my` in
front of `%stash{'monsters'} = @monsters`. Probably a copy-paste error
or something like that.
>> my %stash> {}>> my @monsters = <> fingfangfoom>>
tingler>>> [godzilla grendel wormface blob fingfangfoom tingler]>>
Hi Joe, I tested the code you put up using the REPL, and I have to
start off by saying that I was unable to reproduce your results,
specifically where you creat the "@m" array. This could be a
REPL-specific issue, or a version-specific issue (mine is 2019.07.1
Perl 6.d):
mbook:~ homedir$ perl6
To
FWIW, it's the same as if you would do:
my @a = ^10;
my $b = @a;
dd $b; # Array $b = $[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
A scalar can only hold a single value. That single value in $b is an Array.
But it is still a single value (shown in the output by the $[ ] construct.
Adding .lis
22 matches
Mail list logo