Hash from two arrays: Difference between revisions

From Rosetta Code
Content added Content deleted
mNo edit summary
(revert spam)
Line 1: Line 1:
{{task}}
[http://cincydj.ifrance.com/images/small/gay-porno/ gay porno] [http://competitio.ifrance.com/incesti-foto/ incesti foto] [http://tulip123456.ifrance.com/topic/porno-britny.html porno britny] [http://bakadeshi.ifrance.com/img/styles/foto-eros.html foto eros del mese] [http://adrienneee.ifrance.com/blog/sogni-erotici/ sogni erotici] [http://bakadeshi.ifrance.com/img/styles/foto-della.html foto della pornostar fanny bravo] [http://tulip123456.ifrance.com/topic/futbolistas-desnudos.html futbolistas desnudos] [http://furrysoap.ifrance.com/ursula-andrews/ ursula andrews desnuda] [http://tulip123456.ifrance.com/topic/tette-famose.html tette famose] [http://adrienneee.ifrance.com/blog/vecchio-foto/ vecchio foto nuda] [http://mi-kkiechang.ifrance.com/paola-perissi.html paola perissi] [http://elixirium.ifrance.com/lib/video-gratis.html video gratis tette grosse] [http://competitio.ifrance.com/donne-sexi/ donne sexi anziane nude] [http://jnk713.ifrance.com/img/styles/www-sesso/ www sesso coit] [http://elixirium.ifrance.com/lib/karaoke-dascaricare.html karaoke dascaricare gratis] [http://end14april.ifrance.com/blog/gf-sexy.html gf sexy] [http://competitio.ifrance.com/stefania-nobile/ stefania nobile porno] [http://tulip123456.ifrance.com/topic/federica-mori.html federica mori pornostar] [http://doctorpinkerton.ifrance.com/lib/paola-fellatio/ paola fellatio] [http://doctorpinkerton.ifrance.com/lib/gambe-donne/ gambe donne foto] [http://cincydj.ifrance.com/images/small/sex-pregnent/ sex pregnent] [http://jnk713.ifrance.com/img/styles/sexo-de/ sexo de hentai y manga] [http://mi-kkiechang.ifrance.com/giochi-per.html giochi per sole donne di tutti igeneri] [http://spirit-psyki.ifrance.com/lib/solo-ieri.html solo ieri eros] [http://mi-kkiechang.ifrance.com/simpson-xxx.html simpson xxx] [http://furrysoap.ifrance.com/gay-boy/ gay boy] [http://jnk713.ifrance.com/img/styles/sessantenni-maschi/ sessantenni maschi gay] [http://mi-kkiechang.ifrance.com/ombre-sul.html ombre sul canal grande] [http://cincydj.ifrance.com/images/small/download-video/ download video porno gratis] [http://bakadeshi.ifrance.com/img/styles/il-calendario.html il calendario di rossella brescia nuda] [http://adrienneee.ifrance.com/blog/lolita-nuda/ lolita nuda pic] [http://bakadeshi.ifrance.com/img/styles/fotto-di.html fotto di orge] [http://furrysoap.ifrance.com/clip-porno/ clip porno gratis] [http://bakadeshi.ifrance.com/img/styles/telefoni-erotici.html telefoni erotici] [http://end14april.ifrance.com/blog/donne-scopano.html donne scopano con animali] [http://bakadeshi.ifrance.com/img/styles/doppie-penetrazioni.html doppie penetrazioni anali] [http://adrienneee.ifrance.com/blog/scopate-gratis/ scopate gratis] [http://spirit-psyki.ifrance.com/lib/calendarios-sexis.html calendarios sexis] [http://cincydj.ifrance.com/images/small/porno-video/ porno video video] [http://spirit-psyki.ifrance.com/lib/catherine-falgayrac.html catherine falgayrac] [http://mi-kkiechang.ifrance.com/testo-italiano.html testo italiano fuck you right back] [http://adrienneee.ifrance.com/blog/gay-sex/ gay sex sites] [http://spirit-psyki.ifrance.com/lib/giochi-programma.html giochi programma gratis download] [http://mi-kkiechang.ifrance.com/donne-con.html donne con cavalli] [http://cincydj.ifrance.com/images/small/xxx-school/ xxx school girls] [http://doctorpinkerton.ifrance.com/lib/iran-free/ iran free sexy girl] [http://tulip123456.ifrance.com/topic/hard-fuck.html hard fuck] [http://end14april.ifrance.com/blog/donne-famose.html donne famose nudi] [http://doctorpinkerton.ifrance.com/lib/sesso-s/ sesso s le sue posizioni] [http://cincydj.ifrance.com/images/small/disegni-di/ disegni di draghi gratis] [http://jnk713.ifrance.com/img/styles/giochi-assai/ giochi assai erotici] [http://spirit-psyki.ifrance.com/lib/eamon-testo.html eamon testo fuck it] [http://elixirium.ifrance.com/lib/sexy-free.html sexy free pics] [http://cincydj.ifrance.com/images/small/xxxporno/ xxxporno] [http://spirit-psyki.ifrance.com/lib/donne-toscana.html donne toscana] [http://mi-kkiechang.ifrance.com/i-want.html i want fuck some girls] [http://cincydj.ifrance.com/images/small/foto-porno/ foto porno milly carlucci] [http://spirit-psyki.ifrance.com/lib/donne-mature.html donne mature sesso gratis] [http://jnk713.ifrance.com/img/styles/il-mondo/ il mondo di una cover girl] [http://bakadeshi.ifrance.com/img/styles/kasex.html kasex] [http://doctorpinkerton.ifrance.com/lib/motocicliste-sexy/ motocicliste sexy] [http://end14april.ifrance.com/blog/www-nuda.html www nuda com] [http://end14april.ifrance.com/blog/porno-e.html porno e sesso] [http://jnk713.ifrance.com/img/styles/moana-pozzi/ moana pozzi ex pornostar] [http://cincydj.ifrance.com/images/small/filmati-porno/ filmati porno con cavalli] [http://bakadeshi.ifrance.com/img/styles/beastsex-com.html beastsex com] [http://adrienneee.ifrance.com/blog/ragazze-sotto/ ragazze sotto zero] [http://adrienneee.ifrance.com/blog/porno-victoria/ porno victoria principal] [http://end14april.ifrance.com/blog/transex-a.html transex a milano] [http://cincydj.ifrance.com/images/small/www-hotsex/ www hotsex com] [http://doctorpinkerton.ifrance.com/lib/www-sexxxxx/ www sexxxxx com my] [http://doctorpinkerton.ifrance.com/lib/trans-prosperosa/ trans prosperosa] [http://furrysoap.ifrance.com/pornos-video/ pornos video] [http://adrienneee.ifrance.com/blog/sex-and/ sex and semen] [http://mi-kkiechang.ifrance.com/elisabetta-canalis.html elisabetta canalis max] [http://competitio.ifrance.com/foto-ragazze/ foto ragazze annunci] [http://end14april.ifrance.com/blog/scaricare-the.html scaricare the sims 2 gratis] [http://tulip123456.ifrance.com/topic/analogico-modem.html analogico modem trust] [http://tulip123456.ifrance.com/topic/invio-messaggi.html invio messaggi gratis] [http://tulip123456.ifrance.com/topic/sexy-teacher.html sexy teacher] [http://bakadeshi.ifrance.com/img/styles/ravvedimento-operoso.html ravvedimento operoso omessa presentazion] [http://doctorpinkerton.ifrance.com/lib/fucki-t/ fucki t you right back] [http://cincydj.ifrance.com/images/small/free-pornos/ free pornos] [http://bakadeshi.ifrance.com/img/styles/aeros-the.html aeros. the stage show] [http://doctorpinkerton.ifrance.com/lib/annunci-di/ annunci di donne mature 50 anni con foto] [http://jnk713.ifrance.com/img/styles/brayton-george/ brayton, george] [http://cincydj.ifrance.com/images/small/www-xxxl/ www xxxl com] [http://adrienneee.ifrance.com/blog/tatuaggi-ali/ tatuaggi ali paola e chiara] [http://spirit-psyki.ifrance.com/lib/cinema-per.html cinema per adulti] [http://spirit-psyki.ifrance.com/lib/sexy-anal.html sexy anal] [http://jnk713.ifrance.com/img/styles/filmati-amatoriali/ filmati amatoriali gratis] [http://doctorpinkerton.ifrance.com/lib/foto-casalegno/ foto casalegno nuda] [http://spirit-psyki.ifrance.com/lib/immagini-sexy.html immagini sexy di uomini] [http://spirit-psyki.ifrance.com/lib/video-gioco.html video gioco scaricare gratis] [http://cincydj.ifrance.com/images/small/gay-gratuito/ gay gratuito] [http://jnk713.ifrance.com/img/styles/transessuali-annunci/ transessuali annunci] [http://mi-kkiechang.ifrance.com/sesso-free.html sesso free senza scaricare] [http://doctorpinkerton.ifrance.com/lib/sessoestremocom/ sessoestremo.com] [http://doctorpinkerton.ifrance.com/lib/racconti-erotici/ racconti erotici per masturbazione] [http://end14april.ifrance.com/blog/ranma-xxx.html ranma xxx] [http://competitio.ifrance.com/crazy-for/ crazy for this girl] [http://jnk713.ifrance.com/img/styles/hard-core/ hard core sex] [http://competitio.ifrance.com/foto-di/ foto di nudo maschile] [http://bakadeshi.ifrance.com/img/styles/pamela-anderson.html pamela anderson tommy lee video] [http://spirit-psyki.ifrance.com/lib/sabrina-ferili.html sabrina ferili nuda] [http://bakadeshi.ifrance.com/img/styles/donne-nude.html donne nude foto porno] [http://elixirium.ifrance.com/lib/grassi-nudi.html grassi nudi] [http://adrienneee.ifrance.com/blog/giochi-porno/ giochi porno oline gratis] [http://spirit-psyki.ifrance.com/lib/veronica-ze.html veronica ze] [http://tulip123456.ifrance.com/topic/la-doppia.html la doppia vita di veronica] [http://mi-kkiechang.ifrance.com/sms-gratis.html sms gratis uno al giorno] [http://elixirium.ifrance.com/lib/donnegrasse.html donnegrasse] [http://elixirium.ifrance.com/lib/donne-che.html donne che scopano con vecchi] [http://furrysoap.ifrance.com/donne-tg/ donne tg] [http://end14april.ifrance.com/blog/foto-tette.html foto tette scopate] [http://adrienneee.ifrance.com/blog/free-xxx/ free xxx videos] [http://tulip123456.ifrance.com/topic/s-o.html s o sex com] [http://doctorpinkerton.ifrance.com/lib/giocare-gratis/ giocare gratis] [http://adrienneee.ifrance.com/blog/giochi-sexy/ giochi sexy men] [http://furrysoap.ifrance.com/pagine-sexy/ pagine sexy amatoriali] [http://cincydj.ifrance.com/images/small/sexy-culi/ sexy culi] [http://spirit-psyki.ifrance.com/lib/seni-grandi.html seni grandi foto gratis] [http://mi-kkiechang.ifrance.com/bracciale-snake.html bracciale snake] [http://competitio.ifrance.com/testo-in/ testo in italiano di fuck it di eamon] [http://doctorpinkerton.ifrance.com/lib/www-iran/ www iran sex com] [http://furrysoap.ifrance.com/video-porno/ video porno di pamela anderson e tommy l] [http://bakadeshi.ifrance.com/img/styles/racconti-erotici.html racconti erotici in famiglia] [http://cincydj.ifrance.com/images/small/veronica-gf/ veronica gf5] [http://adrienneee.ifrance.com/blog/lesbo-tettone/ lesbo tettone] [http://bakadeshi.ifrance.com/img/styles/pianale-accessori.html pianale accessori per autoradio] [http://furrysoap.ifrance.com/georges-corraface/ georges corraface] [http://spirit-psyki.ifrance.com/lib/codec-indeo.html codec indeo per xp gratis] [http://adrienneee.ifrance.com/blog/pamela-anderson/ pamela anderson video porno] [http://adrienneee.ifrance.com/blog/gallerie-foto/ gallerie foto hard gratis] [http://adrienneee.ifrance.com/blog/una-sexy/ una sexy giapponese] [http://cincydj.ifrance.com/images/small/eros-gay/ eros gay] [http://furrysoap.ifrance.com/sexl/ sexl] [http://adrienneee.ifrance.com/blog/video-porno/ video porno di selen] [http://cincydj.ifrance.com/images/small/annuncio-ragazze/ annuncio ragazze single brescia] [http://doctorpinkerton.ifrance.com/lib/free-sex/ free sex fig] [http://tulip123456.ifrance.com/topic/erosramazzoti.html erosramazzoti] [http://adrienneee.ifrance.com/blog/grande-fratello/ grande fratello xxx] [http://cincydj.ifrance.com/images/small/adult-tetris/ adult tetris] [http://doctorpinkerton.ifrance.com/lib/ffilm-porno/ ffilm porno] [http://end14april.ifrance.com/blog/video-games.html video games porno sex] [http://spirit-psyki.ifrance.com/lib/nuda-ewa.html nuda ewa sonnet videos] [http://bakadeshi.ifrance.com/img/styles/assessorato-al.html assessorato al turismo] [http://spirit-psyki.ifrance.com/lib/video-musica.html video musica porno] [http://tulip123456.ifrance.com/topic/gay-free.html gay free video] [http://furrysoap.ifrance.com/letizia-bruni/ letizia bruni video gratis] [http://mi-kkiechang.ifrance.com/foto-di.html foto di trish stratus nuda] [http://furrysoap.ifrance.com/incesto-vicenza/ incesto vicenza] [http://mi-kkiechang.ifrance.com/house-gratis.html house gratis] [http://cincydj.ifrance.com/images/small/porno-mature/ porno mature gratis] [http://tulip123456.ifrance.com/topic/meneguzzi-nuda.html meneguzzi nuda] [http://end14april.ifrance.com/blog/midi-file.html midi file da scaricare gratis] [http://jnk713.ifrance.com/img/styles/fotos-de/ fotos de desnudos de galilea montijo] [http://cincydj.ifrance.com/images/small/dandini-nuda/ dandini nuda] [http://doctorpinkerton.ifrance.com/lib/le-notti/ le notti porno nel mondo n. 2] [http://elixirium.ifrance.com/lib/giovanna-rei.html giovanna rei sexy] [http://bakadeshi.ifrance.com/img/styles/fotos-sexis.html fotos sexis] [http://furrysoap.ifrance.com/veronica-rega/ veronica rega gf5] [http://jnk713.ifrance.com/img/styles/jessica-gayle/ jessica gayle pics] [http://elixirium.ifrance.com/lib/talking-snake.html talking snake] [http://bakadeshi.ifrance.com/img/styles/tette-belle.html tette belle] [http://cincydj.ifrance.com/images/small/donne-con/ donne con numero di telefono] [http://doctorpinkerton.ifrance.com/lib/george-harrison/ george harrison. music box biographical collection] [http://jnk713.ifrance.com/img/styles/gaydar-it/ gaydar it] [http://tulip123456.ifrance.com/topic/ragazze-nane.html ragazze nane] [http://cincydj.ifrance.com/images/small/donne-che/ donne che fanno pipi] [http://bakadeshi.ifrance.com/img/styles/ragazze.html ragazze] [http://tulip123456.ifrance.com/topic/canal.html canal 58] [http://tulip123456.ifrance.com/topic/katia-pedrotti.html katia pedrotti foto sexy nuda] [http://adrienneee.ifrance.com/blog/download-gratis/ download gratis udp player] [http://adrienneee.ifrance.com/blog/donne-nude/ donne nude foto] [http://bakadeshi.ifrance.com/img/styles/ssesso.html ssesso] [http://cincydj.ifrance.com/images/small/sesso-con/ sesso con insegnanti] [http://spirit-psyki.ifrance.com/lib/indian-girls.html indian girls] [http://spirit-psyki.ifrance.com/lib/foto-free.html foto free sexy] [http://jnk713.ifrance.com/img/styles/filmati-erotici/ filmati erotici gratuiti] [http://competitio.ifrance.com/party-porno/ party porno bocchinare] [http://elixirium.ifrance.com/lib/scarica-gratis.html scarica gratis giochi pc] [http://end14april.ifrance.com/blog/www-sesso.html www sesso gratis canalis] [http://end14april.ifrance.com/blog/brad-pitt.html brad pitt nudo naked] [http://mi-kkiechang.ifrance.com/web-sexy.html web sexy] [http://cincydj.ifrance.com/images/small/donne-single/ donne single] [http://doctorpinkerton.ifrance.com/lib/callgirls/ callgirls] [http://jnk713.ifrance.com/img/styles/tette-mostruose/ tette mostruose] [http://furrysoap.ifrance.com/quattro-ragazze/ quattro ragazze coraggiose] [http://bakadeshi.ifrance.com/img/styles/videos-sexuales.html videos sexuales] [http://cincydj.ifrance.com/images/small/katie-holmes/ katie holmes nuda] [http://elixirium.ifrance.com/lib/parole-di.html parole di compleanno gratis] [http://spirit-psyki.ifrance.com/lib/sex-orgy.html sex orgy] [http://competitio.ifrance.com/giochi-gratis/ giochi gratis da salvare] [http://mi-kkiechang.ifrance.com/scaricare-film.html scaricare film dvd gratis] [http://spirit-psyki.ifrance.com/lib/kanguroo-girls.html kanguroo girls] [http://mi-kkiechang.ifrance.com/canalis-foto.html canalis foto] [http://spirit-psyki.ifrance.com/lib/bred-pitt.html bred pitt nudo] [http://mi-kkiechang.ifrance.com/ministerosalute-it.html ministerosalute it] [http://cincydj.ifrance.com/images/small/calendario-playboy/ calendario playboy 1999] {{task}}


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)
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)
Line 44: Line 44:
end loop;
end loop;
for I in Key_List'range loop
for I in Key_List'range loop
Ada.Text_Io.Put_Line(To_String(Key_List(I))
Ada.Text_Io.Put_Line(To_String(Key_List(I)) & " => " &
To_String(Hash.Element(Key_List(I))));
end loop;
end Hash_Map_Test;

==[[C plus plus|C++]]==
[[Category:C plus plus]]
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 sharp|C#]]==
[[Category:C sharp]]
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]]==
[[Category:D]]

char[][] keys = ["one", "two", "three"]
int[] values = [1, 2, 3];
int[char[]] hash;
foreach(i,k; keys)
hash[k] = values[i];
==[[E]]==
[[Category:E]]

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

==[[Haskell]]==
[[Category:Haskell]]
'''Interpreter:''' [[GHC|GHCi]] 6.6
import Data.Map
makeMap ks vs = fromList $ zip ks vs
mymap = makeMap ['a','b','c'] [1,2,3]

==[[JavaScript]]==
[[Category:JavaScript]]

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


==[[OCaml]]==
[[Category: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]]==
[[Category: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]]==

[[Category: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]]==
[[Category: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]]==

[[Category:Python]]
'''Interpreter:''' [[Python]] 2.5
<pre>
keys = ['a', 'b', 'c']
values = [1, 2, 3]
hash = dict(zip(keys, values))

# Lazily:
from itertools import izip
hash = dict(izip(keys, values))
</pre>

==[[Ruby]]==
[[Category: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]] ==
[[Category: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]] ==
[[Category: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 "<i>lists</i> 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 }

Revision as of 19:24, 26 August 2007

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)

Ada

Compiler: GNAT GPL 2007

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;

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 }