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
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]
>
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
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
> Though I've no idea what those colons are/are not doing.
Those are "colon pairs" (which I've relearned around three times now...):
https://docs.raku.org/language/glossary#index-entry-Colon_Pair
Except for this colon:
%stash.append: (rocks => @rocks);
Which is a short hand for this:
Thanks, this is indeed the trick:
> Ok, clear enough. This is as simple as:
> %stash.append: (:@greek);
On 3/17/20, Vadim Belman wrote:
> My reply to Joseph went off the list too. I copy it over here. Here is the
> key quote from Joseph's email I was answering to:
>
> So doing that with
Vadim seems to have provided the definitive answer:
Ok, clear enough. This is as simple as:
> %stash.append: (:@greek);
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'}
My reply to Joseph went off the list too. I copy it over here. Here is the key
quote from Joseph's email I was answering to:
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,
Vadim clarified for us, off-list:
> So, you basically needed:
my %h = :a(1); %h.append: (:b(2));
> Am I correct?
I think so, I mean, I believe the append method(?) for hashes would solve the
problem the "whatever star" was attempted to be used for - so:
> my @monsters = << godzilla grendel
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?
>> 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
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
> 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
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?
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
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
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"
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
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
There might not be much to say about this, I just though I'd
mention that I keep getting re-surprised by basics with Raku,
like this one, where first I stash an array away in a hash and
later try to pull the array out again:
my %stash;
my @monsters =
<< godzilla grendel wormface
24 matches
Mail list logo