Hash from two arrays: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
Line 1: Line 1:
{{task}}
[http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/AshleighRodriquez/jybuhi.htm viagra online] [http://dcc.syr.edu/dforum/message.asp?MessageID=48916 phentermine online] [http://devel.linux.duke.edu/bugzilla/attachment.cgi?id=256 ultram online] [http://bugzilla.internet2.edu/attachment.cgi?id=760 diazepam online] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/JustinJonson/xyhyj.htm cheap rivotril] [http://ccfl.unl.edu/twiki/pub/Main/SummersFrank/vuru.htm ativan online] [http://ccfl.unl.edu/twiki/pub/Main/AlexCarneyham/hykidi.htm clonazepam online] [http://www.hollins.edu/ubb/Forum32/HTML/000281.html tramadol online] [http://devel.linux.duke.edu/bugzilla/attachment.cgi?id=253 buy carisoprodol] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=26 cheap meridia] [http://ist.greenville.edu/drupal/files/dydyse.html valium online] [http://ccfl.unl.edu/twiki/pub/Main/HermanHogg/rukexo.htm cheap zoloft] [http://ist.greenville.edu/drupal/files/kuxer.html cyclobenzaprine online] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/SelmaJeremy/cybo.htm sonyericsson ringtones] [http://ccfl.unl.edu/twiki/pub/Main/MarthaBell/nezyz.htm flexeril online] [http://ccfl.unl.edu/twiki/pub/Main/SummersFrank/lyfogo.htm online xanax] [http://ccfl.unl.edu/twiki/pub/Main/MarthaBell/wofo.htm free nokia ringtones] [http://grid.unimelb.edu.au/twiki/pub/Main/JamesMiller/sonuhop.htm valium online] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/RobertSmith/pevesuz.htm wellbutrin online] [http://ccfl.unl.edu/twiki/pub/Main/HermanHogg/rypel.htm sildenafil] [http://www.hollins.edu/ubb/Forum32/HTML/000339.html zoloft online] [http://devel.linux.duke.edu/bugzilla/attachment.cgi?id=251 cheap valium] [http://www.hollins.edu/ubb/Forum32/HTML/000384.html free midi ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000290.html cialis online] [http://bugzilla.internet2.edu/attachment.cgi?id=744 free qwest ringtones] [http://devel.linux.duke.edu/bugzilla/attachment.cgi?id=266 real ringtones] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/PhillipRewers/kino.htm free real ringtones] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/SelmaJeremy/belizyl.htm free polyphonic ringtones] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/PhillipRewers/dijuxoj.htm tracfone ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000322.html ultracet online] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=28 norco online] [http://dcc.syr.edu/dforum/message.asp?MessageID=49072 polyphonic ringtones] [http://bugzilla.internet2.edu/attachment.cgi?id=732 buy levitra] [http://ccfl.unl.edu/twiki/pub/Main/AlexCarneyham/woguked.htm cheap ambien] [http://www.hollins.edu/ubb/Forum32/HTML/000334.html cheap sildenafil] [http://www.hollins.edu/ubb/Forum32/HTML/000333.html cheap prozac] [http://ist.greenville.edu/drupal/files/rucod.html xanax] [http://www.hollins.edu/ubb/Forum32/HTML/000343.html ortho online] [http://ist.greenville.edu/drupal/files/wypy.html funny ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000296.html cheap vicodin] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/BilyKroft/koky.htm phentermine online] [http://bugzilla.internet2.edu/attachment.cgi?id=740 norco online] [http://ccfl.unl.edu/twiki/pub/Main/HermanHogg/figog.htm lortab online] [http://ccfl.unl.edu/twiki/pub/Main/AlexCarneyham/kozu.htm cheap hydrocodone] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=24 ativan online] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/AshleighRodriquez/godufit.htm paxil online] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/SelmaJeremy/sevuv.htm free mono ringtones] [http://bugzilla.internet2.edu/attachment.cgi?id=755 valium] [http://grid.unimelb.edu.au/twiki/pub/Main/JamesMiller/sicofyr.htm cheap tramadol] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/BettySmith/kuvodoj.htm cheap zoloft] [http://dcc.syr.edu/dforum/message.asp?MessageID=49034 hydrocodone] [http://dcc.syr.edu/dforum/message.asp?MessageID=49022 viagra online] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/BettySmith/niwupu.htm cheap albuterol] [http://ccfl.unl.edu/twiki/pub/Main/MarthaBell/gedenu.htm rivotril online] [http://ccfl.unl.edu/twiki/pub/Main/PaulinePauline/dyzoko.htm verizon ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000314.html lorazepam online] [http://grid.unimelb.edu.au/twiki/pub/Main/JackLawson/rejyvob.htm order viagra] [http://www.hollins.edu/ubb/Forum32/HTML/000325.html order didrex] [http://ccfl.unl.edu/twiki/pub/Main/SummersFrank/kuzypuh.htm phentermine online] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/SelmaJeremy/nofelyr.htm free cingular ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000389.html free ericsson ringtones] [http://dcc.syr.edu/dforum/message.asp?MessageID=49077 free sony ericsson ringtones] [http://devel.linux.duke.edu/bugzilla/attachment.cgi?id=267 free motorola ringtones] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/JustinJonson/kuluc.htm zanaflex online] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/JustinJonson/gekiw.htm cheap flexeril] [http://www.hollins.edu/ubb/Forum32/HTML/000369.html samsung ringtones] [http://devel.linux.duke.edu/bugzilla/attachment.cgi?id=259 free nokia ringtones] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=22 cheap diazepam] [http://www.hollins.edu/ubb/Forum32/HTML/000376.html sonyericsson ringtones] [http://ist.greenville.edu/drupal/files/devowy.html lorazepam online] [http://ist.greenville.edu/drupal/files/role.html free real ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000351.html but rivotril] [http://www.hollins.edu/ubb/Forum32/HTML/000312.html buy ambien] [http://www.hollins.edu/ubb/Forum32/HTML/000337.html albuterol online] [http://dcc.syr.edu/dforum/message.asp?MessageID=49018 cialis online] [http://dcc.syr.edu/dforum/message.asp?MessageID=49035 ambien online] [http://bugzilla.internet2.edu/attachment.cgi?id=746 free sagem ringtones] [http://dcc.syr.edu/dforum/message.asp?MessageID=49025 norco online] [http://ist.greenville.edu/drupal/files/puweso.html order meridia] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/RobertSmith/cibedis.htm nexium online] [http://ccfl.unl.edu/twiki/pub/Main/PaulinePauline/zivy.htm mono ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000352.html hgh online] [http://bugzilla.internet2.edu/attachment.cgi?id=754 cheap ultram] [http://ist.greenville.edu/drupal/files/ruwyze.html ativan online] [http://ccfl.unl.edu/twiki/pub/Main/AlexCarneyham/linefej.htm wellbutrin online] [http://ccfl.unl.edu/twiki/pub/Main/MarthaBell/rofev.htm free ringtones] [http://ist.greenville.edu/drupal/files/pobiwi.html cheap diazepam] [http://bugzilla.internet2.edu/attachment.cgi?id=751 sprint ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000345.html lortab online] [http://grid.unimelb.edu.au/twiki/pub/Main/JamesMiller/bopogo.htm buy carisoprodol] [http://dcc.syr.edu/dforum/message.asp?MessageID=49071 verizon ringtones] [http://ccfl.unl.edu/twiki/pub/Main/HermanHogg/relu.htm clomid online] [http://ccfl.unl.edu/twiki/pub/Main/AlexCarneyham/sifus.htm cheap xenical] [http://bugzilla.internet2.edu/attachment.cgi?id=759 xanax online] [http://www.hollins.edu/ubb/Forum32/HTML/000360.html free free ringtones] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=43 cheap wellbutrin] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=15 buy carisoprodol] [http://cyrusimap.web.cmu.edu/twiki/bin/view/Main/DikVolirev free tracfone ringtones] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/SherylDiu/popuxo.htm sharp ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000291.html ativan online] [http://ist.greenville.edu/drupal/files/covubi.html buy norco] [http://ccfl.unl.edu/twiki/pub/Main/PaulinePauline/wetule.htm free sony ericsson ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000362.html free tracfone ringtones] [http://dcc.syr.edu/dforum/message.asp?MessageID=49075 sonyericsson ringtones] [http://dcc.syr.edu/dforum/message.asp?MessageID=48901 cheap soma] [http://www.hollins.edu/ubb/Forum32/HTML/000293.html order meridia] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/JustinJonson/gelol.htm buy zyban] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/AshleighRodriquez/rigyse.htm online clonazepam] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/SelmaJeremy/kohe.htm music ringtones] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/RobertSmith/pybex.htm cheap cyclobenzaprine] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/AshleighRodriquez/pero.htm ativan online] [http://ccfl.unl.edu/twiki/pub/Main/AlexCarneyham/huvixu.htm paxil online] [http://grid.unimelb.edu.au/twiki/pub/Main/JackLawson/nunuso.htm cheap ativan] [http://dcc.syr.edu/dforum/message.asp?MessageID=49046 nexium] [http://ccfl.unl.edu/twiki/pub/Main/MarthaBell/cohufyk.htm free mp3 ringtones] [http://ccfl.unl.edu/twiki/pub/Main/MarthaBell/vekorot.htm hgh online] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/RobertSmith/kuky.htm buy propecia] [http://devel.linux.duke.edu/bugzilla/attachment.cgi?id=268 free samsung ringtones] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/BettySmith/kyzefe.htm ortho] [http://ccfl.unl.edu/twiki/pub/Main/MarthaBell/wydyty.htm but zyban] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/SherylDiu/gefu.htm free punk ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000340.html lipitor online] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/SherylDiu/ruviti.htm free mtv ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000346.html cheap vigrx] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=41 lorazepam online] [http://cyrusimap.web.cmu.edu/twiki/bin/view/Main/RingoFloper fioricet online] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/RobertSmith/tuju.htm lorazepam online] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/SherylDiu/sypev.htm free cool ringtones] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=30 qwest ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000289.html cheap diazepam] [http://ccfl.unl.edu/twiki/pub/Main/HermanHogg/kuhufid.htm tenuate online] [http://dcc.syr.edu/dforum/message.asp?MessageID=49028 clonazepam online] [http://dcc.syr.edu/dforum/message.asp?MessageID=49015 buy fioricet] [http://bugzilla.internet2.edu/attachment.cgi?id=725 cheap ativan] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=36 clonazepam online] [http://ccfl.unl.edu/twiki/pub/Main/HermanHogg/zivesup.htm propecia online] [http://dcc.syr.edu/dforum/message.asp?MessageID=49061 mp3 ringtones] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/RobertSmith/donyki.htm buy hydrocodone] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/BilyKroft/hywi.htm cheap carisoprodol] [http://dcc.syr.edu/dforum/message.asp?MessageID=49051 cheap didrex] [http://www.hollins.edu/ubb/Forum32/HTML/000300.html online levitra] [http://bugzilla.internet2.edu/attachment.cgi?id=738 nextel ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000356.html cheap zyban] [http://bugzilla.internet2.edu/attachment.cgi?id=736 mp3 ringtones] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=38 levitra online] [http://www.hollins.edu/ubb/Forum32/HTML/000341.html buy celexa] [http://grid.unimelb.edu.au/twiki/pub/Main/NoriJohn/rupo.htm free tracfone ringtones] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=27 adipex online] [http://dcc.syr.edu/dforum/message.asp?MessageID=49056 free ringtones] [http://ccfl.unl.edu/twiki/pub/Main/PaulinePauline/wypo.htm music ringtones] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=29 nextel ringtones] [http://dcc.syr.edu/dforum/message.asp?MessageID=49054 free nokia ringtones] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/AshleighRodriquez/putyh.htm cheap levitra] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/PhillipRewers/wozone.htm free ringtones] [http://dcc.syr.edu/dforum/message.asp?MessageID=49049 cheap propecia] [http://www.hollins.edu/ubb/Forum32/HTML/000320.html cheap nexium] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/BilyKroft/piwyh.htm cialis] [http://devel.linux.duke.edu/bugzilla/attachment.cgi?id=250 buy soma] [http://devel.linux.duke.edu/bugzilla/attachment.cgi?id=263 free mp3 ringtones] [http://cyrusimap.web.cmu.edu/twiki/bin/view/Main/BillKukin tramadol online] [http://ist.greenville.edu/drupal/files/xuxuw.html cheap phentermine] [http://dcc.syr.edu/dforum/message.asp?MessageID=49059 funny ringtones] [http://ccfl.unl.edu/twiki/pub/Main/AlexCarneyham/zupu.htm nexium online] [http://www.hollins.edu/ubb/Forum32/HTML/000316.html wellbutrin online] [http://ccfl.unl.edu/twiki/pub/Main/SummersFrank/runuz.htm adipex online] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=23 cheap cialis] [http://ist.greenville.edu/drupal/files/rire.html cheap ultram] [http://ccfl.unl.edu/twiki/pub/Main/PaulinePauline/hulupeg.htm alltel ringtones] [http://grid.unimelb.edu.au/twiki/pub/Main/NoriJohn/nywytew.htm free funny ringtones] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/PhillipRewers/cevo.htm free funny ringtones] [http://ccfl.unl.edu/twiki/pub/Main/HermanHogg/nidexyk.htm cheap celexa] [http://ccfl.unl.edu/twiki/pub/Main/PaulinePauline/pelyvug.htm free real ringtones] [http://cyrusimap.web.cmu.edu/twiki/bin/view/Main/LeonPokert free free ringtones] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=33 samsung ringtones] [http://bugzilla.internet2.edu/attachment.cgi?id=757 viagra online] [http://grid.unimelb.edu.au/twiki/pub/Main/JamesMiller/pusug.htm diazepam online] [http://grid.unimelb.edu.au/twiki/pub/Main/JamesMiller/byvuf.htm cheap ultram] [http://bugzilla.internet2.edu/attachment.cgi?id=758 vicodin online] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=35 buy alprazolam] [http://ist.greenville.edu/drupal/files/tego.html propecia online] [http://devel.linux.duke.edu/bugzilla/attachment.cgi?id=257 buy diazepam] [http://www.hollins.edu/ubb/Forum32/HTML/000364.html mp3 ringtones] [http://bugzilla.internet2.edu/attachment.cgi?id=733 meridia online] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/AshleighRodriquez/xyvibi.htm meridia online] [http://www.hollins.edu/ubb/Forum32/HTML/000359.html free nokia ringtones] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/AshleighRodriquez/lyxex.htm adipex online] [http://ist.greenville.edu/drupal/files/huduc.html cheap xenical] [http://ist.greenville.edu/drupal/files/nezy.html online hydrocodone] [http://ccfl.unl.edu/twiki/pub/Main/PaulinePauline/kyxud.htm free cingular ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000375.html free sagem ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000286.html xanax online] [http://www.hollins.edu/ubb/Forum32/HTML/000338.html cheap lisinopril] [http://ccfl.unl.edu/twiki/pub/Main/MarthaBell/wybuk.htm funny ringtones] [http://dcc.syr.edu/dforum/message.asp?MessageID=48909 valium online] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=51 free tracfone ringtones] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/PhillipRewers/gotu.htm samsung ringtones] [http://dcc.syr.edu/dforum/message.asp?MessageID=49020 ativan online] [http://www.hollins.edu/ubb/Forum32/HTML/000386.html free kyocera ringtones] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=19 xanax] [http://grid.unimelb.edu.au/twiki/pub/Main/NoriJohn/xehepoc.htm free mp3 ringtones] {{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)


==[[C plus plus|C ]]==
==[[C plus plus|C++]]==
[[Category:C plus plus]]
[[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).
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
'''Compiler:''' g++ 4.0.2


#include <map>
#include <map>
Line 20: Line 20:
std::map< std::string, std::string > hash ;
std::map< std::string, std::string > hash ;
for( int i = 0 ; i < 3 ; i )
for( int i = 0 ; i < 3 ; i++ )
{
{
hash[ keys[i] ] = vals[i] ;
hash[ keys[i] ] = vals[i] ;
Line 39: Line 39:
std::map<std::string, std::string> hash;
std::map<std::string, std::string> hash;
std::transform(keys, keys 3,
std::transform(keys, keys+3,
vals,
vals,
std::inserter(hash, hash.end()),
std::inserter(hash, hash.end()),
Line 58: Line 58:
}
}
for( int i = 0; i < arg_length; i ){
for( int i = 0; i < arg_length; i++ ){
h.add( arg_keys[i], arg_values[i] );
h.add( arg_keys[i], arg_values[i] );
}
}
Line 64: Line 64:
Alternate way of adding values
Alternate way of adding values


for( int i = 0; i < arg_length; i ){
for( int i = 0; i < arg_length; i++ ){
h[ arg_keys[i] ] = arg_values[i];
h[ arg_keys[i] ] = arg_values[i];
}
}
Line 167: Line 167:
$values = array(1, 2, 3);
$values = array(1, 2, 3);
$hash = array();
$hash = array();
for ($idx = 0; $idx < count($keys); $idx ) {
for ($idx = 0; $idx < count($keys); $idx++) {
$hash[$keys[$idx]] = $values[$idx];
$hash[$keys[$idx]] = $values[$idx];
}
}

Revision as of 05:23, 6 May 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)

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 }