A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
3
Task/Hash-from-two-arrays/0DESCRIPTION
Normal file
3
Task/Hash-from-two-arrays/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
Using two Arrays of equal length, create a Hash object where the elements from one array (the keys) are linked to the elements of the other (the values)
|
||||
|
||||
Related task: [[Associative arrays/Creation]]
|
||||
2
Task/Hash-from-two-arrays/1META.yaml
Normal file
2
Task/Hash-from-two-arrays/1META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
note: Basic language learning
|
||||
8
Task/Hash-from-two-arrays/AWK/hash-from-two-arrays.awk
Normal file
8
Task/Hash-from-two-arrays/AWK/hash-from-two-arrays.awk
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
$ awk 'BEGIN{split("one two three",a);
|
||||
split("1 2 3",b);
|
||||
for(i=1;i in a;i++){c[a[i]]=b[i]};
|
||||
for(i in c)print i,c[i]
|
||||
}'
|
||||
three 3
|
||||
two 2
|
||||
one 1
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
package
|
||||
{
|
||||
public class MyClass
|
||||
{
|
||||
public static function main():Void
|
||||
{
|
||||
var hash:Object = new Object();
|
||||
var keys:Array = new Array("a", "b", "c");
|
||||
var values:Array = new Array(1, 2, 3);
|
||||
|
||||
for (var i:int = 0; i < keys.length(); i++)
|
||||
hash[keys[i]] = values[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
43
Task/Hash-from-two-arrays/Ada/hash-from-two-arrays.ada
Normal file
43
Task/Hash-from-two-arrays/Ada/hash-from-two-arrays.ada
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
with Ada.Strings.Hash;
|
||||
with Ada.Containers.Hashed_Maps;
|
||||
with Ada.Text_Io;
|
||||
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
|
||||
|
||||
procedure Hash_Map_Test is
|
||||
function Equivalent_Key (Left, Right : Unbounded_String) return Boolean is
|
||||
begin
|
||||
return Left = Right;
|
||||
end Equivalent_Key;
|
||||
|
||||
function Hash_Func(Key : Unbounded_String) return Ada.Containers.Hash_Type is
|
||||
begin
|
||||
return Ada.Strings.Hash(To_String(Key));
|
||||
end Hash_Func;
|
||||
|
||||
package My_Hash is new Ada.Containers.Hashed_Maps(Key_Type => Unbounded_String,
|
||||
Element_Type => Unbounded_String,
|
||||
Hash => Hash_Func,
|
||||
Equivalent_Keys => Equivalent_Key);
|
||||
|
||||
type String_Array is array(Positive range <>) of Unbounded_String;
|
||||
|
||||
Hash : My_Hash.Map;
|
||||
Key_List : String_Array := (To_Unbounded_String("foo"),
|
||||
To_Unbounded_String("bar"),
|
||||
To_Unbounded_String("val"));
|
||||
|
||||
Element_List : String_Array := (To_Unbounded_String("little"),
|
||||
To_Unbounded_String("miss"),
|
||||
To_Unbounded_String("muffet"));
|
||||
|
||||
begin
|
||||
for I in Key_List'range loop
|
||||
Hash.Insert(Key => (Key_List(I)),
|
||||
New_Item => (Element_List(I)));
|
||||
end loop;
|
||||
for I in Key_List'range loop
|
||||
Ada.Text_Io.Put_Line(To_String(Key_List(I)) & " => " &
|
||||
To_String(Hash.Element(Key_List(I))));
|
||||
end loop;
|
||||
|
||||
end Hash_Map_Test;
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
use std, array, hash
|
||||
|
||||
let keys = @["hexadecimal" "decimal" "octal" "binary"]
|
||||
let values = @[0xa 11 014 0b1101] (: 10 11 12 13 :)
|
||||
let hash = new hash of int
|
||||
for each val int i from 0 to 3
|
||||
hash[keys[i]] = values[i]
|
||||
del hash hash
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
array1 := ["two", "three", "apple"]
|
||||
array2 := [2, 3, "fruit"]
|
||||
hash := {}
|
||||
Loop % array1.maxIndex()
|
||||
hash[array1[A_Index]] := array2[A_Index]
|
||||
MsgBox % hash["apple"] "`n" hash["two"]
|
||||
20
Task/Hash-from-two-arrays/BBC-BASIC/hash-from-two-arrays.bbc
Normal file
20
Task/Hash-from-two-arrays/BBC-BASIC/hash-from-two-arrays.bbc
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
DIM array1$(4) : array1$() = "0", "1", "2", "3", "4"
|
||||
DIM array2$(4) : array2$() = "zero", "one", "two", "three", "four"
|
||||
|
||||
FOR index% = 0 TO DIM(array1$(),1)
|
||||
PROCputdict(mydict$, array2$(index%), array1$(index%))
|
||||
NEXT
|
||||
PRINT FNgetdict(mydict$, "3")
|
||||
END
|
||||
|
||||
DEF PROCputdict(RETURN dict$, value$, key$)
|
||||
IF dict$ = "" dict$ = CHR$(0)
|
||||
dict$ += key$ + CHR$(1) + value$ + CHR$(0)
|
||||
ENDPROC
|
||||
|
||||
DEF FNgetdict(dict$, key$)
|
||||
LOCAL I%, J%
|
||||
I% = INSTR(dict$, CHR$(0) + key$ + CHR$(1))
|
||||
IF I% = 0 THEN = "" ELSE I% += LEN(key$) + 2
|
||||
J% = INSTR(dict$, CHR$(0), I%)
|
||||
= MID$(dict$, I%, J% - I%)
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
two three apple:?arr1
|
||||
& 2 3 fruit:?arr2
|
||||
& new$hash:?H
|
||||
& whl
|
||||
' ( !arr1:%?k ?arr1
|
||||
& !arr2:%?v ?arr2
|
||||
& (H..insert)$(!k.!v)
|
||||
)
|
||||
& (H..forall)$out
|
||||
& ;
|
||||
10
Task/Hash-from-two-arrays/Brat/hash-from-two-arrays.brat
Normal file
10
Task/Hash-from-two-arrays/Brat/hash-from-two-arrays.brat
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
zip = { keys, values |
|
||||
h = [:]
|
||||
keys.each_with_index { key, index |
|
||||
h[key] = values[index]
|
||||
}
|
||||
|
||||
h
|
||||
}
|
||||
|
||||
p zip [1 2 3] [:a :b :c] #Prints [1: a, 2: b, 3: c]
|
||||
16
Task/Hash-from-two-arrays/C++/hash-from-two-arrays-1.cpp
Normal file
16
Task/Hash-from-two-arrays/C++/hash-from-two-arrays-1.cpp
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#include <map>
|
||||
#include <string>
|
||||
|
||||
int
|
||||
main( int argc, char* argv[] )
|
||||
{
|
||||
std::string keys[] = { "1", "2", "3" } ;
|
||||
std::string vals[] = { "a", "b", "c" } ;
|
||||
|
||||
std::map< std::string, std::string > hash ;
|
||||
|
||||
for( int i = 0 ; i < 3 ; i++ )
|
||||
{
|
||||
hash[ keys[i] ] = vals[i] ;
|
||||
}
|
||||
}
|
||||
17
Task/Hash-from-two-arrays/C++/hash-from-two-arrays-2.cpp
Normal file
17
Task/Hash-from-two-arrays/C++/hash-from-two-arrays-2.cpp
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#include <map> // for std::map
|
||||
#include <algorithm> // for std::transform
|
||||
#include <string> // for std::string
|
||||
#include <utility> // for std::make_pair
|
||||
|
||||
int main()
|
||||
{
|
||||
std::string keys[] = { "one", "two", "three" };
|
||||
std::string vals[] = { "foo", "bar", "baz" };
|
||||
|
||||
std::map<std::string, std::string> hash;
|
||||
|
||||
std::transform(keys, keys+3,
|
||||
vals,
|
||||
std::inserter(hash, hash.end()),
|
||||
std::make_pair<std::string, std::string>);
|
||||
}
|
||||
14
Task/Hash-from-two-arrays/C-sharp/hash-from-two-arrays-1.cs
Normal file
14
Task/Hash-from-two-arrays/C-sharp/hash-from-two-arrays-1.cs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
System.Collections.HashTable h = new System.Collections.HashTable();
|
||||
|
||||
string[] arg_keys = {"foo","bar","val"};
|
||||
string[] arg_values = {"little", "miss", "muffet"};
|
||||
|
||||
//Some basic error checking
|
||||
int arg_length = 0;
|
||||
if ( arg_keys.Length == arg_values.Length ) {
|
||||
arg_length = arg_keys.Length;
|
||||
}
|
||||
|
||||
for( int i = 0; i < arg_length; i++ ){
|
||||
h.add( arg_keys[i], arg_values[i] );
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
for( int i = 0; i < arg_length; i++ ){
|
||||
h[ arg_keys[i] ] = arg_values[i];
|
||||
}
|
||||
115
Task/Hash-from-two-arrays/C/hash-from-two-arrays.c
Normal file
115
Task/Hash-from-two-arrays/C/hash-from-two-arrays.c
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define KeyType const char *
|
||||
#define ValType int
|
||||
|
||||
#define HASH_SIZE 4096
|
||||
|
||||
// hash function useful when KeyType is char * (string)
|
||||
unsigned strhashkey( const char * key, int max)
|
||||
{
|
||||
unsigned h=0;
|
||||
unsigned hl, hr;
|
||||
|
||||
while(*key) {
|
||||
h += *key;
|
||||
hl= 0x5C5 ^ (h&0xfff00000 )>>18;
|
||||
hr =(h&0x000fffff );
|
||||
h = hl ^ hr ^ *key++;
|
||||
}
|
||||
return h % max;
|
||||
}
|
||||
|
||||
typedef struct sHme {
|
||||
KeyType key;
|
||||
ValType value;
|
||||
struct sHme *link;
|
||||
} *MapEntry;
|
||||
|
||||
typedef struct he {
|
||||
MapEntry first, last;
|
||||
} HashElement;
|
||||
|
||||
HashElement hash[HASH_SIZE];
|
||||
|
||||
typedef void (*KeyCopyF)(KeyType *kdest, KeyType ksrc);
|
||||
typedef void (*ValCopyF)(ValType *vdest, ValType vsrc);
|
||||
typedef unsigned (*KeyHashF)( KeyType key, int upperBound );
|
||||
typedef int (*KeyCmprF)(KeyType key1, KeyType key2);
|
||||
|
||||
void HashAddH( KeyType key, ValType value,
|
||||
KeyCopyF copyKey, ValCopyF copyVal, KeyHashF hashKey, KeyCmprF keySame )
|
||||
{
|
||||
unsigned hix = (*hashKey)(key, HASH_SIZE);
|
||||
MapEntry m_ent;
|
||||
|
||||
for (m_ent= hash[hix].first;
|
||||
m_ent && !(*keySame)(m_ent->key,key); m_ent=m_ent->link);
|
||||
if (m_ent) {
|
||||
(*copyVal)(&m_ent->value, value);
|
||||
}
|
||||
else {
|
||||
MapEntry last;
|
||||
MapEntry hme = malloc(sizeof(struct sHme));
|
||||
(*copyKey)(&hme->key, key);
|
||||
(*copyVal)(&hme->value, value);
|
||||
hme->link = NULL;
|
||||
last = hash[hix].last;
|
||||
if (last) {
|
||||
// printf("Dup. hash key\n");
|
||||
last->link = hme;
|
||||
}
|
||||
else
|
||||
hash[hix].first = hme;
|
||||
hash[hix].last = hme;
|
||||
}
|
||||
}
|
||||
|
||||
int HashGetH(ValType *val, KeyType key, KeyHashF hashKey, KeyCmprF keySame )
|
||||
{
|
||||
unsigned hix = (*hashKey)(key, HASH_SIZE);
|
||||
MapEntry m_ent;
|
||||
for (m_ent= hash[hix].first;
|
||||
m_ent && !(*keySame)(m_ent->key,key); m_ent=m_ent->link);
|
||||
if (m_ent) {
|
||||
*val = m_ent->value;
|
||||
}
|
||||
return (m_ent != NULL);
|
||||
}
|
||||
|
||||
void copyStr(const char**dest, const char *src)
|
||||
{
|
||||
*dest = strdup(src);
|
||||
}
|
||||
void copyInt( int *dest, int src)
|
||||
{
|
||||
*dest = src;
|
||||
}
|
||||
int strCompare( const char *key1, const char *key2)
|
||||
{
|
||||
return strcmp(key1, key2) == 0;
|
||||
}
|
||||
|
||||
void HashAdd( KeyType key, ValType value )
|
||||
{
|
||||
HashAddH( key, value, ©Str, ©Int, &strhashkey, &strCompare);
|
||||
}
|
||||
|
||||
int HashGet(ValType *val, KeyType key)
|
||||
{
|
||||
return HashGetH( val, key, &strhashkey, &strCompare);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
static const char * keyList[] = {"red","orange","yellow","green", "blue", "violet" };
|
||||
static int valuList[] = {1,43,640, 747, 42, 42};
|
||||
int ix;
|
||||
|
||||
for (ix=0; ix<6; ix++) {
|
||||
HashAdd(keyList[ix], valuList[ix]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
(zipmap [\a \b \c] [1 2 3])
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
keys = ['a','b','c']
|
||||
values = [1,2,3]
|
||||
map = {}
|
||||
map[key] = values[i] for key, i in keys
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
<cfscript>
|
||||
function makeHash(keyArray, valueArray) {
|
||||
var x = 1;
|
||||
var result = {};
|
||||
for( ; x <= ArrayLen(keyArray); x ++ ) {
|
||||
result[keyArray[x]] = valueArray[x];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
keyArray = ['a', 'b', 'c'];
|
||||
valueArray = [1, 2, 3];
|
||||
map = makeHash(keyArray, valueArray);
|
||||
</cfscript>
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
(defun rosetta-code-hash-from-two-arrays (vector-1 vector-2 &key (test 'eql))
|
||||
(assert (= (length vector-1) (length vector-2)))
|
||||
(let ((table (make-hash-table :test test :size (length vector-1))))
|
||||
(map nil (lambda (k v) (setf (gethash k table) v))
|
||||
vector-1 vector-2)
|
||||
table))
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
(defun rosetta-code-hash-from-two-arrays (vector-1 vector-2 &key (test 'eql))
|
||||
(loop initially (assert (= (length vector-1) (length vector-2)))
|
||||
with table = (make-hash-table :test test :size (length vector-1))
|
||||
for k across vector-1
|
||||
for v across vector-2
|
||||
do (setf (gethash k table) v)
|
||||
finally (return table)))
|
||||
5
Task/Hash-from-two-arrays/D/hash-from-two-arrays.d
Normal file
5
Task/Hash-from-two-arrays/D/hash-from-two-arrays.d
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
import std.array, std.range;
|
||||
|
||||
void main() {
|
||||
auto hash = ["a", "b", "c"].zip([1, 2, 3]).assocArray;
|
||||
}
|
||||
3
Task/Hash-from-two-arrays/E/hash-from-two-arrays.e
Normal file
3
Task/Hash-from-two-arrays/E/hash-from-two-arrays.e
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
def keys := ["one", "two", "three"]
|
||||
def values := [1, 2, 3]
|
||||
__makeMap.fromColumns(keys, values)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
USING: hashtables ;
|
||||
{ "one" "two" "three" } { 1 2 3 } zip >hashtable
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
keys = [ 'a', 'b', 'c', 'd' ]
|
||||
values = [ 1, 2, 3, 4 ]
|
||||
hash = [ => ]
|
||||
for i in [ 0 : keys.len() ]: hash[ keys[ i ] ] = values[ i ]
|
||||
16
Task/Hash-from-two-arrays/Fantom/hash-from-two-arrays.fantom
Normal file
16
Task/Hash-from-two-arrays/Fantom/hash-from-two-arrays.fantom
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
class Main
|
||||
{
|
||||
public static Void main ()
|
||||
{
|
||||
keys := [1,2,3,4,5]
|
||||
values := ["one", "two", "three", "four", "five"]
|
||||
|
||||
// create an empty map
|
||||
map := [:]
|
||||
// add the key-value pairs to it
|
||||
keys.size.times |Int index|
|
||||
{
|
||||
map.add(keys[index], values[index])
|
||||
}
|
||||
}
|
||||
}
|
||||
13
Task/Hash-from-two-arrays/Go/hash-from-two-arrays.go
Normal file
13
Task/Hash-from-two-arrays/Go/hash-from-two-arrays.go
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
keys := []string{"a", "b", "c"}
|
||||
vals := []int{1, 2, 3}
|
||||
hash := map[string]int{}
|
||||
for i, key := range keys {
|
||||
hash[key] = vals[i]
|
||||
}
|
||||
fmt.Println(hash)
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
keys = ['a','b','c']
|
||||
vals = ['aaa', 'bbb', 'ccc']
|
||||
hash = [:]
|
||||
keys.eachWithIndex { key, i ->
|
||||
hash[key] = vals[i]
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
import Data.Map
|
||||
|
||||
makeMap ks vs = fromList $ zip ks vs
|
||||
mymap = makeMap ['a','b','c'] [1,2,3]
|
||||
12
Task/Hash-from-two-arrays/Icon/hash-from-two-arrays.icon
Normal file
12
Task/Hash-from-two-arrays/Icon/hash-from-two-arrays.icon
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
link ximage # to format the structure
|
||||
|
||||
procedure main(arglist) #: demonstrate hash from 2 lists
|
||||
local keylist
|
||||
|
||||
if *arglist = 0 then arglist := [1,2,3,4] # ensure there's a list
|
||||
every put(keylist := [], "key-" || !arglist) # make keys for each entry
|
||||
|
||||
every (T := table())[keylist[ i := 1 to *keylist ]] := arglist[i] # create the hash table
|
||||
|
||||
write(ximage(T)) # show result
|
||||
end
|
||||
1
Task/Hash-from-two-arrays/Ioke/hash-from-two-arrays.ioke
Normal file
1
Task/Hash-from-two-arrays/Ioke/hash-from-two-arrays.ioke
Normal file
|
|
@ -0,0 +1 @@
|
|||
{} addKeysAndValues([:a, :b, :c], [1, 2, 3])
|
||||
1
Task/Hash-from-two-arrays/J/hash-from-two-arrays-1.j
Normal file
1
Task/Hash-from-two-arrays/J/hash-from-two-arrays-1.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
hash=: vals {~ keys&i.
|
||||
19
Task/Hash-from-two-arrays/J/hash-from-two-arrays-2.j
Normal file
19
Task/Hash-from-two-arrays/J/hash-from-two-arrays-2.j
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
keys=: 10?.100
|
||||
vals=: > ;:'zero one two three four five six seven eight nine'
|
||||
hash=: vals {~ keys&i.
|
||||
|
||||
keys
|
||||
46 99 23 62 42 44 12 5 68 63
|
||||
$vals
|
||||
10 5
|
||||
|
||||
hash 46
|
||||
zero
|
||||
hash 99
|
||||
one
|
||||
hash 63 5 12 5 23
|
||||
nine
|
||||
seven
|
||||
six
|
||||
seven
|
||||
two
|
||||
10
Task/Hash-from-two-arrays/Java/hash-from-two-arrays.java
Normal file
10
Task/Hash-from-two-arrays/Java/hash-from-two-arrays.java
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import java.util.HashMap;
|
||||
public static void main(String[] args){
|
||||
String[] keys= {"a", "b", "c"};
|
||||
int[] vals= {1, 2, 3};
|
||||
HashMap<String, Integer> hash= new HashMap<String, Integer>();
|
||||
|
||||
for(int i= 0; i < keys.length; i++){
|
||||
hash.put(keys[i], vals[i]);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
var keys = ['a', 'b', 'c'];
|
||||
var values = [1, 2, 3];
|
||||
var map = {};
|
||||
for(var i in keys) {
|
||||
map[ keys[i] ] = values[i];
|
||||
}
|
||||
10
Task/Hash-from-two-arrays/K/hash-from-two-arrays-1.k
Normal file
10
Task/Hash-from-two-arrays/K/hash-from-two-arrays-1.k
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
a: `zero `one `two / symbols
|
||||
b: 0 1 2
|
||||
|
||||
d:. a,'b / create the dictionary
|
||||
.((`zero;0;)
|
||||
(`one;1;)
|
||||
(`two;2;))
|
||||
|
||||
d[`one]
|
||||
1
|
||||
19
Task/Hash-from-two-arrays/K/hash-from-two-arrays-2.k
Normal file
19
Task/Hash-from-two-arrays/K/hash-from-two-arrays-2.k
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
keys: !10 / 0..9
|
||||
split:{1_'(&x=y)_ x:y,x}
|
||||
vals:split["zero one two three four five six seven eight nine";" "]
|
||||
|
||||
s:{`$$x} / convert to symbol
|
||||
d:. (s'keys),'s'vals
|
||||
.((`"0";`zero;)
|
||||
(`"1";`one;)
|
||||
(`"2";`two;)
|
||||
(`"3";`three;)
|
||||
(`"4";`four;)
|
||||
(`"5";`five;)
|
||||
(`"6";`six;)
|
||||
(`"7";`seven;)
|
||||
(`"8";`eight;)
|
||||
(`"9";`nine;))
|
||||
|
||||
$d[s 1] / leading "$" converts back to string
|
||||
"one"
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
: >table 2 compress -1 transpose ;
|
||||
['one 'two 'three 'four] [1 2 3 4] >table
|
||||
6
Task/Hash-from-two-arrays/Lua/hash-from-two-arrays.lua
Normal file
6
Task/Hash-from-two-arrays/Lua/hash-from-two-arrays.lua
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
function(keys,values)
|
||||
local t = {}
|
||||
for i=1, #keys do
|
||||
t[keys[i]] = values[i]
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
Map[(Hash[Part[#, 1]] = Part[#, 2]) &,
|
||||
Transpose[{{1, 2, 3}, {"one", "two", "three"}}]]
|
||||
|
||||
?? Hash
|
||||
->Hash[1]=one
|
||||
->Hash[2]=two
|
||||
->Hash[3]=three
|
||||
3
Task/Hash-from-two-arrays/PHP/hash-from-two-arrays-1.php
Normal file
3
Task/Hash-from-two-arrays/PHP/hash-from-two-arrays-1.php
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
$keys = array('a', 'b', 'c');
|
||||
$values = array(1, 2, 3);
|
||||
$hash = array_combine($keys, $values);
|
||||
6
Task/Hash-from-two-arrays/PHP/hash-from-two-arrays-2.php
Normal file
6
Task/Hash-from-two-arrays/PHP/hash-from-two-arrays-2.php
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
$keys = array('a', 'b', 'c');
|
||||
$values = array(1, 2, 3);
|
||||
$hash = array();
|
||||
for ($idx = 0; $idx < count($keys); $idx++) {
|
||||
$hash[$keys[$idx]] = $values[$idx];
|
||||
}
|
||||
4
Task/Hash-from-two-arrays/Perl/hash-from-two-arrays-1.pl
Normal file
4
Task/Hash-from-two-arrays/Perl/hash-from-two-arrays-1.pl
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
my @keys = qw(a b c);
|
||||
my @vals = (1, 2, 3);
|
||||
my %hash;
|
||||
@hash{@keys} = @vals;
|
||||
2
Task/Hash-from-two-arrays/Perl/hash-from-two-arrays-2.pl
Normal file
2
Task/Hash-from-two-arrays/Perl/hash-from-two-arrays-2.pl
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
use List::MoreUtils qw(zip);
|
||||
my %hash = zip @keys, @vals;
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
(let (Keys '(one two three) Values (1 2 3))
|
||||
(mapc println
|
||||
(mapcar cons Keys Values) ) )
|
||||
19
Task/Hash-from-two-arrays/Prolog/hash-from-two-arrays.pro
Normal file
19
Task/Hash-from-two-arrays/Prolog/hash-from-two-arrays.pro
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
% this one with side effect hash table creation
|
||||
|
||||
:-dynamic hash/2.
|
||||
|
||||
make_hash([],[]).
|
||||
make_hash([H|Q],[H1|Q1]):-
|
||||
assert(hash(H,H1)),
|
||||
make_hash(Q,Q1).
|
||||
|
||||
:-make_hash([un,deux,trois],[[a,b,c],[d,e,f],[g,h,i]])
|
||||
|
||||
|
||||
% this one without side effects
|
||||
|
||||
make_hash_pure([],[],[]).
|
||||
make_hash_pure([H|Q],[H1|Q1],[hash(H,H1)|R]):-
|
||||
make_hash_pure(Q,Q1,R).
|
||||
|
||||
:-make_hash_pure([un,deux,trois],[[a,b,c],[d,e,f],[g,h,i]],L),findall(M,(member(M,L),assert(M)),L).
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
keys = ['a', 'b', 'c']
|
||||
values = [1, 2, 3]
|
||||
hash = dict(zip(keys, values))
|
||||
|
||||
# Lazily, Python 2.3+, not 3.x:
|
||||
from itertools import izip
|
||||
hash = dict(izip(keys, values))
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
keys = ['a', 'b', 'c']
|
||||
values = [1, 2, 3]
|
||||
hash = {key: value for key, value in zip(keys, values)}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
keys = ['a', 'b', 'c']
|
||||
values = [1, 2, 3]
|
||||
hash = {}
|
||||
for k,v in zip(keys, values):
|
||||
hash[k] = v
|
||||
41
Task/Hash-from-two-arrays/Python/hash-from-two-arrays-4.py
Normal file
41
Task/Hash-from-two-arrays/Python/hash-from-two-arrays-4.py
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
>>> class Hashable(object):
|
||||
def __hash__(self):
|
||||
return id(self) ^ 0xBEEF
|
||||
|
||||
|
||||
>>> my_inst = Hashable()
|
||||
>>> my_int = 1
|
||||
>>> my_complex = 0 + 1j
|
||||
>>> my_float = 1.2
|
||||
>>> my_string = "Spam"
|
||||
>>> my_bool = True
|
||||
>>> my_unicode = u'Ham'
|
||||
>>> my_list = ['a', 7]
|
||||
>>> my_tuple = ( 0.0, 1.4 )
|
||||
>>> my_set = set(my_list)
|
||||
>>> def my_func():
|
||||
pass
|
||||
|
||||
>>> class my_class(object):
|
||||
pass
|
||||
|
||||
>>> keys = [my_inst, my_tuple, my_int, my_complex, my_float, my_string,
|
||||
my_bool, my_unicode, frozenset(my_set), tuple(my_list),
|
||||
my_func, my_class]
|
||||
>>> values = range(12)
|
||||
>>> d = dict(zip(keys, values))
|
||||
>>> for key, value in d.items(): print key, ":", value
|
||||
|
||||
1 : 6
|
||||
1j : 3
|
||||
Ham : 7
|
||||
Spam : 5
|
||||
(0.0, 1.3999999999999999) : 1
|
||||
frozenset(['a', 7]) : 8
|
||||
1.2 : 4
|
||||
('a', 7) : 9
|
||||
<function my_func at 0x0128E7B0> : 10
|
||||
<class '__main__.my_class'> : 11
|
||||
<__main__.Hashable object at 0x012AFC50> : 0
|
||||
>>> # Notice that the key "True" disappeared, and its value got associated with the key "1"
|
||||
>>> # This is because 1 == True in Python, and dictionaries cannot have two equal keys
|
||||
8
Task/Hash-from-two-arrays/R/hash-from-two-arrays.r
Normal file
8
Task/Hash-from-two-arrays/R/hash-from-two-arrays.r
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
# Set up hash table
|
||||
keys <- c("John Smith", "Lisa Smith", "Sam Doe", "Sandra Dee", "Ted Baker")
|
||||
values <- c(152, 1, 254, 152, 153)
|
||||
names(values) <- keys
|
||||
# Get value corresponding to a key
|
||||
values["Sam Doe"] # vals["Sam Doe"]
|
||||
# Get all keys corresponding to a value
|
||||
names(values)[values==152] # "John Smith" "Sandra Dee"
|
||||
18
Task/Hash-from-two-arrays/REXX/hash-from-two-arrays.rexx
Normal file
18
Task/Hash-from-two-arrays/REXX/hash-from-two-arrays.rexx
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
/*REXX program demonstrate hashing of a stemmed array (from a key). */
|
||||
/*names of the 9 regular polygons*/
|
||||
values='triangle quadrilateral pentagon hexagon heptagon octagon nonagon decagon dodecagon'
|
||||
keys ='thuhree vour phive sicks zeaven ate nein den duzun'
|
||||
|
||||
/*superflous blanks added to humorous keys just 'cause it looks prettier*/
|
||||
call hash values,keys /*hash the keys to the values. */
|
||||
parse arg query . /*what was specified on cmd line.*/
|
||||
if query=='' then exit /*nothing, then let's leave Dodge*/
|
||||
pad=left('',30) /*used for padding the display. */
|
||||
say 'key:' query pad "value:" hash.query /*show & tell some stuff.*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────HASH subroutine─────────────────────*/
|
||||
hash: procedure expose hash.; parse arg v,k,hash.
|
||||
do j=1 until map=''; map=word(k,j)
|
||||
hash.map=word(v,j)
|
||||
end /*j*/
|
||||
return
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
(make-hash
|
||||
(map cons
|
||||
'("a" "b" "c" "d")
|
||||
'(1 2 3 4)))
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
(define (connect keys vals)
|
||||
(for/hash ([k keys] [v vals]) (values k v)))
|
||||
;; Example:
|
||||
(connect #("a" "b" "c" "d") #(1 2 3 4))
|
||||
14
Task/Hash-from-two-arrays/Ruby/hash-from-two-arrays-1.rb
Normal file
14
Task/Hash-from-two-arrays/Ruby/hash-from-two-arrays-1.rb
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
keys=['hal',666,[1,2,3]]
|
||||
vals=['ibm','devil',123]
|
||||
|
||||
if RUBY_VERSION >= '1.8.7'
|
||||
# Easy way, but needs Ruby 1.8.7 or later.
|
||||
hash = Hash[keys.zip(vals)]
|
||||
else
|
||||
hash = keys.zip(vals).inject({}) {|h, kv| h.store(*kv); h }
|
||||
end
|
||||
|
||||
p hash # => {"hal"=>"ibm", 666=>"devil", [1, 2, 3]=>123}
|
||||
|
||||
#retrieve the value linked to the key [1,2,3]
|
||||
puts hash[ [1,2,3] ] # => 123
|
||||
9
Task/Hash-from-two-arrays/Ruby/hash-from-two-arrays-2.rb
Normal file
9
Task/Hash-from-two-arrays/Ruby/hash-from-two-arrays-2.rb
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
class Array
|
||||
def zip_hash(other)
|
||||
Hash[*(0...self.size).inject([]) { |arr, ix|
|
||||
arr.push(self[ix], other[ix]) } ]
|
||||
end
|
||||
end
|
||||
|
||||
hash = %W{ a b c }.zip_hash( %W{ 1 2 3 } )
|
||||
p hash # => {"a"=>"1", "b"=>"2", "c"=>"3"}
|
||||
23
Task/Hash-from-two-arrays/Sather/hash-from-two-arrays.sa
Normal file
23
Task/Hash-from-two-arrays/Sather/hash-from-two-arrays.sa
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
class ZIPPER{K,E} is
|
||||
zip(k:ARRAY{K}, e:ARRAY{E}) :MAP{K, E}
|
||||
pre k.size = e.size
|
||||
is
|
||||
m :MAP{K, E} := #;
|
||||
loop m[k.elt!] := e.elt!; end;
|
||||
return m;
|
||||
end;
|
||||
end;
|
||||
|
||||
class MAIN is
|
||||
|
||||
main is
|
||||
keys:ARRAY{STR} := |"one", "three", "four"|;
|
||||
values:ARRAY{INT} := |1, 3, 4|;
|
||||
m ::= ZIPPER{STR,INT}::zip(keys, values);
|
||||
loop
|
||||
#OUT + m.pair! + " ";
|
||||
end;
|
||||
#OUT + "\n";
|
||||
end;
|
||||
|
||||
end;
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
val keys = Array(1, 2, 3)
|
||||
val values = Array("A", "B", "C")
|
||||
val map = keys.zip(values).toMap
|
||||
2
Task/Hash-from-two-arrays/Scheme/hash-from-two-arrays.ss
Normal file
2
Task/Hash-from-two-arrays/Scheme/hash-from-two-arrays.ss
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
(define (lists->hash-table keys values . rest)
|
||||
(apply alist->hash-table (map cons keys values) rest))
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
Array extend [
|
||||
dictionaryWithValues: array [ |d|
|
||||
d := Dictionary new.
|
||||
1 to: ((self size) min: (array size)) do: [:i|
|
||||
d at: (self at: i) put: (array at: i).
|
||||
].
|
||||
^ d
|
||||
]
|
||||
].
|
||||
|
||||
|
||||
({ 'red' . 'one' . 'two' }
|
||||
dictionaryWithValues: { '#ff0000'. 1. 2 }) displayNl.
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
Dictionary
|
||||
withKeys:#('one' 'two' 'three')
|
||||
andValues:#('eins' 'zwei' 'drei')
|
||||
|
|
@ -0,0 +1 @@
|
|||
Dictionary withAssociations:{ 'one'->1 . 'two'->2 . 'three'->3 }
|
||||
4
Task/Hash-from-two-arrays/Tcl/hash-from-two-arrays-1.tcl
Normal file
4
Task/Hash-from-two-arrays/Tcl/hash-from-two-arrays-1.tcl
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
set keys [list fred bob joe]
|
||||
set values [list barber plumber tailor]
|
||||
array set arr {}
|
||||
foreach a $keys b $values { set arr($a) $b }
|
||||
3
Task/Hash-from-two-arrays/Tcl/hash-from-two-arrays-2.tcl
Normal file
3
Task/Hash-from-two-arrays/Tcl/hash-from-two-arrays-2.tcl
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
foreach a $keys b $values {
|
||||
dict set jobs $a $b
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue