Hash from two arrays

Revision as of 09:53, 11 May 2007 by 210.0.140.99 (talk)

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

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.8.8

Short

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

Mid Length

my @keys = ('a', 'b', 'c');
my @vals = (1, 2, 3);
my %hash;
$hash{$keys[$_]} = $vals[$_] for (0 .. $#keys);

Long

my @keys = ('a', 'b', 'c');
my @vals = (1, 2, 3);
my %hash;
foreach my $idx (0..$#keys){
  $hash{$keys[$idx]} = $vals[$idx];
}

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

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 }