Hash from two arrays: Difference between revisions

m
→‎{{header|Perl 6}}: update for automated testing
(→‎{{header|Ruby}}: removed custom method since it is built in nowadays)
m (→‎{{header|Perl 6}}: update for automated testing)
Line 1,153:
Using the "zipwith" meta-operator on the <tt>=></tt> pair composer:
 
{{works with|rakudo|2015-09-132018.03}}
<lang perl6>my @keys = <a b c d e>;
my @values = ^5;
 
my %hash = @keys Z=> @values;</lang>
 
Alternatively, by assigning to a hash slice:
 
#Alternatively, by assigning to a hash slice:
<lang perl6>my %hash;
%hash{@keys} = @values;</lang>
 
Or to create an anonymous hash:
 
# Or to create an anonymous hash:
<lang perl6>%( @keys Z=> @values )</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 } # Will fail if the lists differ in length</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:
# If you wish to enforce equal lengths, you can use a strict hyperoperator instead:
 
quietly # suppress warnings about unused hash
<lang perl6>{ @keys »=>« @values }; # Will fail if the lists differ in length</lang>
 
=={{header|Phix}}==
10,339

edits