Jump to content

Hash from two arrays: Difference between revisions

(Added EchoLisp)
Line 1,056:
 
=={{header|Perl 6}}==
{{works with|rakudo|2015-09-13}}
<lang perl6>my @keys = <a b c d e>;
my @vals = ^5;
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>;
my %hash = @v Z=> @v.keys;</lang>
 
 
Line 1,071 ⟶ 1,072:
%hash{@keys} = @vals;</lang>
 
To create an anonymous hash value, you can use Z as a "zipwith" metaoperator on the => pair composer:
 
<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:
<lang perl6>{ @keys »=>« @values }</lang>
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.