Hash from two arrays: Difference between revisions
→{{header|langur}}
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
Langurmonkey (talk | contribs) |
||
(13 intermediate revisions by 9 users not shown) | |||
Line 876:
iex(3)> Enum.zip(keys, values) |> Enum.into(Map.new)
%{one: 1, three: 3, two: 2}</syntaxhighlight>
=={{header|Emacs Lisp}}==
<syntaxhighlight lang="lisp">
(let ((keys ["a" "b" "c"])
(values [1 2 3]))
(apply 'vector (cl-loop for i across keys for j across values collect (vector i j))))
</syntaxhighlight>
=={{header|EMal}}==
<syntaxhighlight lang="emal">
List keys = var["hal", 666, int[1,2,3]]
List vals = var["ibm", "devil", 123]
Map hash = keys.zip(vals)
writeLine(hash)
</syntaxhighlight>
{{out}}
<pre>
[hal:ibm,666:devil,[1,2,3]:123]
</pre>
=={{header|Erlang}}==
Line 947 ⟶ 966:
a = new dict[["a", "b", "c"], [1, 2, 3]]
</syntaxhighlight>
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
void local fn DoIt
CFArrayRef keys = @[@"Key1",@"Key2",@"Key3",@"Key4"]
CFArrayRef values = @[@"One",@"Two",@"Three",@"O'Leary"]
CFDictionaryRef dict = fn DictionaryWithObjectsForKeys( values, keys )
NSLog(@"%@",dict)
end fn
fn DoIt
HandleEvents
</syntaxhighlight>
{{out}}
<pre>
{
Key1 = One;
Key2 = Two;
Key3 = Three;
Key4 = "O'Leary";
}
</pre>
=={{header|Gambas}}==
Line 1,258 ⟶ 1,303:
=={{header|langur}}==
=== the easy way ===
<syntaxhighlight lang="langur">writeln(hash
</syntaxhighlight>
Note that
=== a longer way ===
Using the append operator would silently overwrite hash values for matching keys, but the more() function will not.
<syntaxhighlight lang="langur">val .new = foldfrom(
)
Line 1,272 ⟶ 1,318:
{{out}}
<pre>
=={{header|Lasso}}==
Line 2,125 ⟶ 2,171:
two 2
three 3
</pre>
=={{header|RPL}}==
≪ <span style="color:red">2</span> →LIST ≫ '<span style="color:blue">→HASH</span>' STO
≪ OVER <span style="color:red">1</span> GET SWAP POS
SWAP <span style="color:red">2</span> GET
SWAP GET
≫ '<span style="color:blue">HASH→</span>' STO
<span style="color:red">{ "one" "two" "three" } { 1 2 3 }</span> <span style="color:blue">→HASH</span>
DUP '<span style="color:green">MYHASH</span>' STO
<span style="color:green">MYHASH</span> <span style="color:red">2</span> <span style="color:blue">HASH→</span>
{{out}}
<pre>
2: { { 1 2 3 } { "one" "two" "three" } }
1: "two"
</pre>
Line 2,601 ⟶ 2,664:
<pre>[[a; 1]; [b; 2]; [c; 3]]</pre>
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">fn main() {
keys := ["a", "b", "c"]
vals := [1, 2, 3]
Line 2,631 ⟶ 2,694:
=={{header|Wren}}==
Wren's built-in Map class does not guarantee (as here) that iteration order will be the same as the order in which elements were added.
<syntaxhighlight lang="
var values = ["first", "second", "third", "fourth","fifth"]
var hash = {}
Line 2,641 ⟶ 2,704:
{2: second, 1: first, 3: third, 5: fifth, 4: fourth}
</pre>
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">func Code(Str); \Return a simple, perfect hash code for the Keys used here
char Str;
return Str(2) & 7;
int Keys, Values, I, Hash(8);
[Keys:= ["first", "second", "third", "fourth", "fifth", "sixth"];
Values:= [1, 2, 3, 4, 5, 6];
for I:= 0 to 6-1 do
Hash(Code(Keys(I))):= Values(I);
IntOut(0, Hash(Code("second"))); CrLf(0);
IntOut(0, Hash(Code("sixth"))); CrLf(0);
]</syntaxhighlight>
{{out}}
<pre>
2
6
</pre>
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="vb">data "1", "one", "2", "two", "3", "three", "4", "four", "5", "five"
dim keys$(5), values$(5)
dim hash$(5)
for i = 1 to arraysize(keys$(), 1)
read a$, b$
keys$(i) = a$
values$(i) = b$
next i
for i = 1 to arraysize(values$(), 1)
temp = val(keys$(i))
hash$(temp) = values$(i)
next i
for i = 1 to arraysize(hash$(), 1)
print keys$(i), " ", hash$(i)
next i
end</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
=={{header|zkl}}==
|