Hash from two arrays: Difference between revisions

Content added Content deleted
(Added EchoLisp)
Line 1,056: Line 1,056:


=={{header|Perl 6}}==
=={{header|Perl 6}}==
{{works with|rakudo|2015-09-13}}
<lang perl6>my @keys = <a b c d e>;
<lang perl6>my @keys = <a b c d e>;
my @vals = ^5;
my @vals = ^5;
my %hash = @keys Z @vals;</lang>
my %hash = flat @keys Z @vals;</lang>


or using the "zipwith" metaoperaotr on the <tt>=></tt> pair composer:
or just


<lang perl6>my @v = <a b c d e>;
<lang perl6>my @v = <a b c d e>;
my %hash = @v Z @v.keys;</lang>
my %hash = @v Z=> @v.keys;</lang>




Line 1,071: Line 1,072:
%hash{@keys} = @vals;</lang>
%hash{@keys} = @vals;</lang>


To create an anonymous hash value, you can use Z as a "zipwith" metaoperator on the => pair composer:
To create an anonymous hash value:


<lang perl6>{ <a b c d e> Z=> ^5 }</lang>
<lang perl6>%( <a b c d e> Z=> ^5 )</lang>
All of these zip forms trim the result to the length of the shorter of their two input lists. If you wish to enforce equal lengths, you can use a strict hyperoperator instead:
All of these zip forms trim the result to the length of the shorter of their two input lists. If you wish to enforce equal lengths, you can use a strict hyperoperator instead:
<lang perl6>{ @keys »=>« @values }</lang>
<lang perl6>{ @keys »=>« @values }</lang>