Hash from two arrays: Difference between revisions
Content added Content deleted
Langurmonkey (talk | contribs) |
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
||
Line 110: | Line 110: | ||
<pre>#{ four: 4, one: 1, three: 3, two: 2 }</pre> |
<pre>#{ four: 4, one: 1, three: 3, two: 2 }</pre> |
||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
Line 119: | Line 118: | ||
hash[array1[A_Index]] := array2[A_Index] |
hash[array1[A_Index]] := array2[A_Index] |
||
MsgBox % hash["apple"] "`n" hash["two"]</lang> |
MsgBox % hash["apple"] "`n" hash["two"]</lang> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
Awk arrays are used for both lists and hash maps. |
Awk arrays are used for both lists and hash maps. |
||
Line 316: | Line 316: | ||
return 0; |
return 0; |
||
}</lang> |
}</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}}== |
=={{header|C sharp}}== |
||
Line 389: | Line 361: | ||
} |
} |
||
}</lang> |
}</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}}== |
=={{header|Ceylon}}== |
||
Line 857: | Line 857: | ||
Donald aged 70 |
Donald aged 70 |
||
</pre> |
</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}}== |
=={{header|Lang5}}== |
||
Line 893: | Line 879: | ||
{{out}} |
{{out}} |
||
<pre>h{"d": 4, "a": 1, "b": 2, "c": 3}</pre> |
<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}}== |
=={{header|LFE}}== |
||
Line 937: | Line 937: | ||
end |
end |
||
end</lang> |
end</lang> |
||
=={{header|M2000 Interpreter}}== |
=={{header|M2000 Interpreter}}== |
||
Line 1,114: | Line 1,113: | ||
Hashed key: 4 Value: 9 |
Hashed key: 4 Value: 9 |
||
Hashed key: 737454 Value: zero</pre> |
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}}== |
=={{header|NetRexx}}== |
||
Line 1,185: | Line 1,202: | ||
return |
return |
||
</lang> |
</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}}== |
=={{header|Nim}}== |
||
Line 1,211: | Line 1,210: | ||
let table = toTable zip(keys, values)</lang> |
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}}== |
=={{header|Oberon-2}}== |
||
Line 1,276: | Line 1,250: | ||
END HashFromArrays. |
END HashFromArrays. |
||
</lang> |
</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}}== |
=={{header|OCaml}}== |
||
Line 1,377: | Line 1,376: | ||
<lang perl>use List::MoreUtils qw(zip); |
<lang perl>use List::MoreUtils qw(zip); |
||
my %hash = zip @keys, @vals;</lang> |
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}}== |
=={{header|Phix}}== |
||
Line 1,490: | Line 1,464: | ||
{= =} forall |
{= =} forall |
||
</lang> |
</lang> |
||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
<lang powershell>function create_hash ([array] $keys, [array] $values) { |
<lang powershell>function create_hash ([array] $keys, [array] $values) { |
||
Line 1,628: | Line 1,603: | ||
(connect #("a" "b" "c" "d") #(1 2 3 4)) |
(connect #("a" "b" "c" "d") #(1 2 3 4)) |
||
</lang> |
</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}}== |
=={{header|Raven}}== |
||
Line 1,956: | Line 1,957: | ||
<pre>$ ./txr hash-from-two.tl |
<pre>$ ./txr hash-from-two.tl |
||
#H(() (c 3) (b 2) (a 1))</pre> |
#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}}== |
=={{header|UnixPipes}}== |
||
Line 1,977: | Line 1,997: | ||
paste -d\ <(cat p.values | sort) <(cat p.keys | sort)</lang> |
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}}== |
=={{header|Ursala}}== |