Hash from two arrays

From Rosetta Code

cheap ultram xanax online cheap cialis free midi ringtones free funny ringtones wellbutrin online order nexium fioricet online jazz ringtones motorola ringtones mtv ringtones cheap cialis lisinopril online nokia ringtones paxil online cheap didrex ultracet online sonyericsson ringtones mono ringtones buy valium diazepam clomid online cheap fioricet cheap paxil kyocera ringtones mp3 ringtones mp3 ringtones cheap alprazolam free alltel ringtones alprazolam online lipitor online hydrocodone online verizon ringtones levitra tramadol online buy hydrocodone sprint ringtones xanax online free real ringtones free tracfone ringtones propecia online order ativan samsung ringtones tramadol online didrex online but clomid lorazepam online free samsung ringtones albuterol online cheap zyban soma online cheap clonazepam paxil online free real ringtones sildenafil online soma online ambien online order wellbutrin order adipex buy propecia free real ringtones diethylpropion online polyphonic ringtones cheap rivotril jazz ringtones but lipitor cheap lorazepam prozac online carisoprodol online free polyphonic ringtones online tenuate cheap valium polyphonic ringtones buy lipitor free free ringtones phentermine online sagem ringtones free free ringtones buy ultracet buy ortho buy vigrx nexium online meridia online free sharp ringtones free music ringtones diazepam online carisoprodol online cheap wellbutrin meridia online free free ringtones buy xanax free mtv ringtones cheap fioricet free mp3 ringtones free nextel ringtones adipex online cheap xanax free sagem ringtones diazepam online cheap clonazepam free polyphonic ringtones cheap carisoprodol buy wellbutrin free kyocera ringtones cheap ativan cheap viagra buy lorazepam cheap ultram kyocera ringtones alprazolam online alltel ringtones ambien online vicodin online motorola ringtones free sony ericsson ringtones punk ringtones buy viagra phentermine cheap xanax ultram online cheap valium free cingular ringtones adipex online cheap cyclobenzaprine propecia online tenuate online carisoprodol online cheap ultracet cheap pharmacy online wwe ringtones cheap diazepam cheap xenical free nokia ringtones qwest ringtones zanaflex online diazepam online free verizon ringtones motorola ringtones order lortab prozac online free ericsson ringtones but ortho cyclobenzaprine online cheap celexa free free ringtones online phentermine cool ringtones cheap prozac qwest ringtones cialis online sprint ringtones nexium online real ringtones sagem ringtones free tracfone ringtones free nokia ringtones punk ringtones mp3 ringtones sony ringtones buy ativan cheap hgh free midi ringtones alltel ringtones cheap flexeril free sony ericsson ringtones free motorola ringtones cheap vicodin free tracfone ringtones buy soma levitra online free motorola ringtones didrex online cheap sildenafil pharmacy online online cheap zoloft hydrocodone online sony ringtones free cingular ringtones online viagra buy paxil buy ambien buy lortab hydrocodone online cheap vigrx ativan online hgh online cheap sildenafil buy viagra ortho online nextel ringtones lisinopril funny ringtones free sony ericsson ringtones vicodin online hydrocodone ativan online mono ringtones online clonazepam levitra sprint ringtones

Task
Hash from two arrays
You are encouraged to solve this task according to the task description, using any language you may know.

Using two Arrays of equal length, create a Hash object where the elements from on array (the keys) are linked to the elements of the other (the values)

C

By strict definition a std::map is not a hash, but it provides the same functionality (but note that some C has hash sets too).

Compiler: g 4.0.2

#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] ;
 }
}

Alternatively:

#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>);
}

C#

 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] ); 
 }

Alternate way of adding values

 for( int i = 0; i < arg_length; i   ){
   h[ arg_keys[i] ] = arg_values[i]; 
 }

D

char[][] keys = ["one", "two", "three"]
int[] values  = [1, 2, 3];

int[char[]] hash;

foreach(i,k; keys)
 hash[k] = values[i];

E

def keys := ["one", "two", "three"]
def values := [1, 2, 3]
__makeMap.fromColumns(keys, values)

Haskell

Interpreter: GHCi 6.6

import Data.Map

makeMap ks vs = fromList $ zip ks vs
mymap = makeMap ['a','b','c'] [1,2,3]

JavaScript

var keys = ['a', 'b', 'c'];
var values = [1, 2, 3];
var map = {};
for(var i in keys) {
  map[ keys[i] ] = values[i];
}


OCaml

The idiomatic solution uses lists rather than arrays.

 let keys = [ "foo"; "bar"; "baz" ]
 and vals = [ 16384; 32768; 65536 ]
 and hash = Hashtbl.create 0;;
 List.iter2 (Hashtbl.add hash) keys vals;;

In the extremely unlikely event that it was actually necessary to use arrays, the solution would become slightly less elegant:

 let keys = [| "foo"; "bar"; "baz" |]
 and vals = [| 16384; 32768; 65536 |]
 and hash = Hashtbl.create 0;;
 for i = 0 to Array.length keys - 1 do
   Hashtbl.add hash keys.(i) vals.(i)
 done;;

In either case, an exception is raised if the inputs are different lengths.

Perl

Interpreter: Perl 5

use List::MoreUtils qw(zip);
my @keys = qw(a b c);
my @vals = (1, 2, 3);
my %hash = zip @keys, @vals;

Using no modules:

my %hash;
@hash{qw(a b c)} = (1, 2, 3);

PHP

PHP 5:

$keys = array('a', 'b', 'c');
$values = array(1, 2, 3);
$hash = array_combine($keys, $values);

PHP 4:

$keys = array('a', 'b', 'c');
$values = array(1, 2, 3);
$hash = array();
for ($idx = 0; $idx < count($keys); $idx  ) {
  $hash[$keys[$idx]] = $values[$idx];
}

Pop11

vars keys = { 1 a b c};
vars vals = { 2 3 valb valc};
vars i;
;;; Create hash table
vars ht = newmapping([], 500, 0, true);
;;; Loop over input arrays (vectors)
for i from 1 to length(keys) do
  vals(i) -> ht(keys(i));
endfor;

Python

Interpreter: Python 2.5

keys = ['a', 'b', 'c']
values = [1, 2, 3]
hash = dict(zip(keys, values))

# Lazily:
from itertools import izip
hash = dict(izip(keys, values))

Ruby

 keys=['hal',666,[1,2,3]]
 vals=['ibm','devil',123]
 hash = Hash[*keys.zip(vals).flatten]
 # now 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

Scala

val keys = Array(1, 2, 3)
val values = Array("A", "B", "C")
val map = Map(keys.zip(values) : _*)
// returns Map(1 -> "A", 2 -> "B", 3 -> "C")
// keys.zip(values) is an array of pairs : Array({1, "A"}, {2, "B"}, {3, "C"})
// Map(...) expects multiple pairs arguments. Syntax ": _*" tells the single argument contains multiple values.

Tcl

Arrays in Tcl are automatically associative, i.e. there are no "not hashed arrays". If we can take "arrays of equal length" to mean "lists of equal length", then the task might look like this:

set keys [list fred bob joe]
set values [list barber plumber tailor]
array set arr {}
foreach a $keys b $values { set $arr($a) $b }