Re: Creating an array of a single hash
> On 1 Jun 2017, at 16:29, Gabor Szabowrote: > > use v6; > > my @x = { name => "Foo" }, { name => "Bar"} > say @x.gist; # [{name => Foo} {name => Bar}] > say @x.^name;# Array > say @x[0].^name; # Hash > > my @y = { name => "Foo" } > say @y; # [name => Foo] > say @y.^name; # Array > say @y[0].^name; # Pair > > my @z = { name => "Foo" },; > say @z; # [{name => Foo}] > say @z.^name; # Array > say @z[0].^name; # Hash > > > In the first example, creating an array of 2 hashes work. > In the second example the listy assignment removes the hashy-ness of > the right hand side. > In the 3rd example adding a comma at the end solves the problem. > > Is this how it is recommended to initiate an array with a single hash? > Is there an operator that forces the assignment to item-assignment? FWIW, this follows out of the 1 argument rule: $ 6 'for { a => 42 } { dd $_ }' :a(42) $ 6 'for { a => 42 }, { b => 666 } { dd $_ }' Hash % = {:a(42)} Hash % = {:b(666)} If only 1 argument is specified, it will be iterated over. And you could consider list assignment also such a case (as it internally calls @x.STORE in that case). Liz
Re: Creating an array of a single hash
On Thu, Jun 1, 2017 at 5:44 PM, Timo Paulssenwrote: > Yeah, you can use the prefix $ to itemize things, like so: > > timo@schmand ~> perl6 -e 'my @y = ${ name => "Foo" }; say @y.gist; > say @y.^name; say @y[0].^name' > [{name => Foo}] > Array > Hash > > HTH > - Timo Thanks. Gabor
Re: Creating an array of a single hash
Yeah, you can use the prefix $ to itemize things, like so: timo@schmand ~> perl6 -e 'my @y = ${ name => "Foo" }; say @y.gist; say @y.^name; say @y[0].^name' [{name => Foo}] Array Hash HTH - Timo
Creating an array of a single hash
use v6; my @x = { name => "Foo" }, { name => "Bar"} say @x.gist; # [{name => Foo} {name => Bar}] say @x.^name;# Array say @x[0].^name; # Hash my @y = { name => "Foo" } say @y; # [name => Foo] say @y.^name; # Array say @y[0].^name; # Pair my @z = { name => "Foo" },; say @z; # [{name => Foo}] say @z.^name; # Array say @z[0].^name; # Hash In the first example, creating an array of 2 hashes work. In the second example the listy assignment removes the hashy-ness of the right hand side. In the 3rd example adding a comma at the end solves the problem. Is this how it is recommended to initiate an array with a single hash? Is there an operator that forces the assignment to item-assignment? Gabor