develooper Front page | perl.perl6.users | Postings from June 2017

Creating an array of a single hash

Thread Next
From:
Gabor Szabo
Date:
June 1, 2017 14:29
Subject:
Creating an array of a single hash
Message ID:
CABe4FJD3cYERi8joTB4-px4S=LRM5_vjUm6V1j=s3+jKPpTHSA@mail.gmail.com
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

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About