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 exit type 'exit' or '^D'
> my %stash
{}
> my @monsters = <<godzilla grendel wormface blob fingfangfoom tingler>>
[godzilla grendel wormface blob fingfangfoom tingler]
> my %stash{'monsters'} = @monsters;
{fingfangfoom => tingler, godzilla => grendel, wormface => blob}
> my @m = %stash{'monsters'};
[(Any)]
> say @m
[(Any)]
> exit

The first thing I notice is when you create a hash from an array using
your method, you pair together 'monsters' from your array. Is this
what you want? Below I show creating a 0-to-5 indexed %stash (hash)
object simply by assigning @monsters.pairs to it. You can back-out
again to an "@-sigilled" variable (see "@monsters_redux" example).
[Note, somewhat surprisingly if you call .WHAT on %stash.values, you
find that it is actually a ".Seq"].

mbook:~ homedir$ perl6
To exit type 'exit' or '^D'
> my @monsters = <<godzilla grendel wormface blob fingfangfoom tingler>>
[godzilla grendel wormface blob fingfangfoom tingler]
> my %stash = @monsters.pairs
{0 => godzilla, 1 => grendel, 2 => wormface, 3 => blob, 4 =>
fingfangfoom, 5 => tingler}
> say %stash.WHAT
(Hash)
> say %stash.values.WHAT
(Seq)
> my @monsters_redux = %stash.values
[godzilla fingfangfoom grendel wormface blob tingler]
> say @monsters_redux.WHAT
(Array)
>

I feel that an advantage of working with hashes/arrays in Raku/Perl6
is they mesh very gracefully, especially using .pairs and .kv methods.
I had occasion to take some duplicate values and count up occurrences:
I simply called the ".Bag" method on an array. I didn't need to go so
far as create a hash, but I could have done so easily. See below for
some ideas, and note the two examples at the end, which differ only in
order:

mbook:~ homedir$ perl6
To exit type 'exit' or '^D'
> my @monsters2 = <<mothra mothra godzilla grendel wormface blob fingfangfoom 
> tingler>>
[mothra mothra godzilla grendel wormface blob fingfangfoom tingler]
> say @monsters2.Bag.pairs.sort(*.values).reverse
(mothra => 2 tingler => 1 blob => 1 godzilla => 1 fingfangfoom => 1
wormface => 1 grendel => 1)
> my %stash2 =  @monsters2.Bag.pairs
{blob => 1, fingfangfoom => 1, godzilla => 1, grendel => 1, mothra =>
2, tingler => 1, wormface => 1}
> for @monsters2.Bag.pairs.kv {.perl.say}
0
:tingler(1)
1
:mothra(2)
2
:blob(1)
3
:fingfangfoom(1)
4
:godzilla(1)
5
:grendel(1)
6
:wormface(1)
> for %stash2.Bag.pairs.kv {.perl.say}
0
:godzilla(1)
1
:fingfangfoom(1)
2
:grendel(1)
3
:wormface(1)
4
:tingler(1)
5
:mothra(2)
6
:blob(1)
>

HTH, Bill.











On Wed, Mar 4, 2020 at 7:36 PM Joseph Brenner <doom...@gmail.com> wrote:
>
> 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 blob fingfangfoom tingler >>;
>     %stash{'monsters'} = @monsters;
>
> Now some time later, when I wanted to extract that array again,
> my first thought was to do this:
>
>     my @m = %stash{'monsters'};
>
> But that doesn't get the original array back, instead you end up
> with the entire original array as the first element of a newly
> created array:
>
>     # [[godzilla grendel wormface blob fingfangfoom tingler]]
>
> Yary Hluchan pointed out that I could slip it out, and get what
> I wanted:
>
>     my @m = | %stash{'monsters'};
>     say @m;
>     # [godzilla grendel wormface blob fingfangfoom tingler]
>
> Though for what I'm doing now, I think it might be better to
> just alias it with the binding operator:
>
>    my @m := %stash{'monsters'};
>
> So, like I said: I don't have any particular questions about this,
> I think I more or less know what's going on... it will be a while
> before I stop tripping over this sort of thing, though.

Reply via email to