Hash from two arrays: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 110:
 
<pre>#{ four: 4, one: 1, three: 3, two: 2 }</pre>
 
 
=={{header|AutoHotkey}}==
Line 119 ⟶ 118:
hash[array1[A_Index]] := array2[A_Index]
MsgBox % hash["apple"] "`n" hash["two"]</lang>
 
=={{header|AWK}}==
Awk arrays are used for both lists and hash maps.
Line 316:
return 0;
}</lang>
 
=={{header|C++}}==
<lang cpp>#include <unordered_map>
#include <string>
 
int main()
{
std::string keys[] = { "1", "2", "3" };
std::string vals[] = { "a", "b", "c" };
std::unordered_map<std::string, std::string> hash;
for( int i = 0 ; i < 3 ; i++ )
hash[ keys[i] ] = vals[i] ;
}</lang>
 
{{libheader|range-v3}}
<lang cpp>#include <range/v3/view/zip.hpp>
#include <unordered_map>
#include <string>
int main()
{
std::string keys[] = { "1", "2", "3" };
std::string vals[] = { "foo", "bar", "baz" };
std::unordered_map<std::string, std::string> hash(ranges::view::zip(keys, vals));
}
</lang>
 
=={{header|C sharp}}==
Line 389 ⟶ 361:
}
}</lang>
 
=={{header|C++}}==
<lang cpp>#include <unordered_map>
#include <string>
 
int main()
{
std::string keys[] = { "1", "2", "3" };
std::string vals[] = { "a", "b", "c" };
std::unordered_map<std::string, std::string> hash;
for( int i = 0 ; i < 3 ; i++ )
hash[ keys[i] ] = vals[i] ;
}</lang>
 
{{libheader|range-v3}}
<lang cpp>#include <range/v3/view/zip.hpp>
#include <unordered_map>
#include <string>
int main()
{
std::string keys[] = { "1", "2", "3" };
std::string vals[] = { "foo", "bar", "baz" };
std::unordered_map<std::string, std::string> hash(ranges::view::zip(keys, vals));
}
</lang>
 
=={{header|Ceylon}}==
Line 857:
Donald aged 70
</pre>
 
=={{header|Lasso}}==
<lang Lasso>local(
array1 = array('a', 'b', 'c'),
array2 = array(1, 2, 3),
hash = map
)
 
loop(#array1 -> size) => {
#hash -> insert(#array1 -> get(loop_count) = #array2 -> get(loop_count))
}
 
#hash</lang>
-> map(a = 1, b = 2, c = 3)
 
=={{header|Lang5}}==
Line 893 ⟶ 879:
{{out}}
<pre>h{"d": 4, "a": 1, "b": 2, "c": 3}</pre>
 
=={{header|Lasso}}==
<lang Lasso>local(
array1 = array('a', 'b', 'c'),
array2 = array(1, 2, 3),
hash = map
)
 
loop(#array1 -> size) => {
#hash -> insert(#array1 -> get(loop_count) = #array2 -> get(loop_count))
}
 
#hash</lang>
-> map(a = 1, b = 2, c = 3)
 
=={{header|LFE}}==
Line 937:
end
end</lang>
 
 
=={{header|M2000 Interpreter}}==
Line 1,114 ⟶ 1,113:
Hashed key: 4 Value: 9
Hashed key: 737454 Value: zero</pre>
 
=={{header|Nemerle}}==
<lang Nemerle>using System;
using System.Console;
using Nemerle.Collections;
using Nemerle.Collections.NCollectionsExtensions;
 
module AssocArray
{
Main() : void
{
def list1 = ["apples", "oranges", "bananas", "kumquats"];
def list2 = [13, 34, 12];
def inventory = Hashtable(ZipLazy(list1, list2));
foreach (item in inventory)
WriteLine("{0}: {1}", item.Key, item.Value);
}
}</lang>
 
=={{header|NetRexx}}==
Line 1,185 ⟶ 1,202:
return
</lang>
 
=={{header|Nemerle}}==
<lang Nemerle>using System;
using System.Console;
using Nemerle.Collections;
using Nemerle.Collections.NCollectionsExtensions;
 
module AssocArray
{
Main() : void
{
def list1 = ["apples", "oranges", "bananas", "kumquats"];
def list2 = [13, 34, 12];
def inventory = Hashtable(ZipLazy(list1, list2));
foreach (item in inventory)
WriteLine("{0}: {1}", item.Key, item.Value);
}
}</lang>
 
=={{header|Nim}}==
Line 1,211 ⟶ 1,210:
 
let table = toTable zip(keys, values)</lang>
 
=={{header|Objeck}}==
 
<lang objeck>
use Structure;
 
bundle Default {
class HashOfTwo {
function : Main(args : System.String[]) ~ Nil {
keys := ["1", "2", "3"];
vals := ["a", "b", "c"];
hash := StringHash->New();
each(i : vals) {
hash->Insert(keys[i], vals[i]->As(Base));
};
}
}
}
</lang>
 
=={{header|Objective-C}}==
 
<lang objc>NSArray *keys = @[@"a", @"b", @"c"];
NSArray *values = @[@1, @2, @3];
NSDictionary *dict = [NSDictionary dictionaryWithObjects:values forKeys:keys];</lang>
 
=={{header|Oberon-2}}==
Line 1,276 ⟶ 1,250:
END HashFromArrays.
</lang>
 
=={{header|Objeck}}==
 
<lang objeck>
use Structure;
 
bundle Default {
class HashOfTwo {
function : Main(args : System.String[]) ~ Nil {
keys := ["1", "2", "3"];
vals := ["a", "b", "c"];
hash := StringHash->New();
each(i : vals) {
hash->Insert(keys[i], vals[i]->As(Base));
};
}
}
}
</lang>
 
=={{header|Objective-C}}==
 
<lang objc>NSArray *keys = @[@"a", @"b", @"c"];
NSArray *values = @[@1, @2, @3];
NSDictionary *dict = [NSDictionary dictionaryWithObjects:values forKeys:keys];</lang>
 
=={{header|OCaml}}==
Line 1,377 ⟶ 1,376:
<lang perl>use List::MoreUtils qw(zip);
my %hash = zip @keys, @vals;</lang>
 
=={{header|Perl 6}}==
 
Using the "zipwith" meta-operator on the <tt>=></tt> pair composer:
 
{{works with|rakudo|2018.03}}
<lang perl6>my @keys = <a b c d e>;
my @values = ^5;
 
my %hash = @keys Z=> @values;
 
 
#Alternatively, by assigning to a hash slice:
%hash{@keys} = @values;
 
 
# Or to create an anonymous hash:
%( @keys Z=> @values );
 
 
# 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:
 
quietly # suppress warnings about unused hash
{ @keys »=>« @values }; # Will fail if the lists differ in length</lang>
 
=={{header|Phix}}==
Line 1,490 ⟶ 1,464:
{= =} forall
</lang>
 
=={{header|PowerShell}}==
<lang powershell>function create_hash ([array] $keys, [array] $values) {
Line 1,628 ⟶ 1,603:
(connect #("a" "b" "c" "d") #(1 2 3 4))
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
 
Using the "zipwith" meta-operator on the <tt>=></tt> pair composer:
 
{{works with|rakudo|2018.03}}
<lang perl6>my @keys = <a b c d e>;
my @values = ^5;
 
my %hash = @keys Z=> @values;
 
 
#Alternatively, by assigning to a hash slice:
%hash{@keys} = @values;
 
 
# Or to create an anonymous hash:
%( @keys Z=> @values );
 
 
# 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:
 
quietly # suppress warnings about unused hash
{ @keys »=>« @values }; # Will fail if the lists differ in length</lang>
 
=={{header|Raven}}==
Line 1,956 ⟶ 1,957:
<pre>$ ./txr hash-from-two.tl
#H(() (c 3) (b 2) (a 1))</pre>
 
=={{header|UNIX Shell}}==
{{works with|Bash|4}}
<lang bash>keys=( foo bar baz )
values=( 123 456 789 )
declare -A hash
 
for (( i = 0; i < ${#keys[@]}; i++ )); do
hash["${keys[i]}"]=${values[i]}
done
 
for key in "${!hash[@]}"; do
printf "%s => %s\n" "$key" "${hash[$key]}"
done</lang>
 
{{out}}
<pre>bar => 456
baz => 789
foo => 123</pre>
 
=={{header|UnixPipes}}==
Line 1,977 ⟶ 1,997:
 
paste -d\ <(cat p.values | sort) <(cat p.keys | sort)</lang>
 
=={{header|UNIX Shell}}==
{{works with|Bash|4}}
<lang bash>keys=( foo bar baz )
values=( 123 456 789 )
declare -A hash
 
for (( i = 0; i < ${#keys[@]}; i++ )); do
hash["${keys[i]}"]=${values[i]}
done
 
for key in "${!hash[@]}"; do
printf "%s => %s\n" "$key" "${hash[$key]}"
done</lang>
 
{{out}}
<pre>bar => 456
baz => 789
foo => 123</pre>
 
=={{header|Ursala}}==
10,327

edits