Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,3 +1,10 @@
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)
{{omit from|BASIC}} <!-- Does not have hash tables or other map structures. -->
{{omit from|PowerBASIC}}
{{omit from|TI-83 BASIC}}
{{omit from|TI-89 BASIC}}
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]]

View file

@ -1,2 +1,4 @@
---
category:
- Data Structures
note: Basic language learning

View file

@ -1,8 +1,11 @@
$ 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
# usage: awk -v list1="i ii iii" -v list2="1 2 3" -f hash2.awk
BEGIN {
if(!list1) list1="one two three"
if(!list2) list2="1 2 3"
split(list1, a);
split(list2, b);
for(i=1;i in a;i++) { c[a[i]] = b[i] };
for(i in c) print i,c[i]
}

View file

@ -1,5 +1,5 @@
import std.array, std.range;
void main() {
auto hash = ["a", "b", "c"].zip([1, 2, 3]).assocArray;
import std.array, std.range;
immutable hash = ["a", "b", "c"].zip([1, 2, 3]).assocArray;
}

View file

@ -1,6 +1,6 @@
var keys = ['a', 'b', 'c'];
var values = [1, 2, 3];
var map = {};
for(var i in keys) {
for(var i = 0; i < keys.length; i += 1) {
map[ keys[i] ] = values[i];
}

View file

@ -0,0 +1,7 @@
function arrToObj(keys, vals) {
var map = {};
keys.forEach(function (key, index) {
map[key] = val[index];
});
return map;
}

View file

@ -0,0 +1,15 @@
function s = StructFromArrays(allKeys, allVals)
% allKeys must be cell array of strings of valid field-names
% allVals can be cell array or array of numbers
% Assumes arrays are same size and valid types
s = struct;
if iscell(allVals)
for k = 1:length(allKeys)
s.(allKeys{k}) = allVals{k};
end
else
for k = 1:length(allKeys)
s.(allKeys{k}) = allVals(k);
end
end
end

View file

@ -0,0 +1,3 @@
A := [1, 2, 3];
B := ["one", "two", three"];
T := table( zip( `=`, A, B ) );

View file

@ -0,0 +1,35 @@
MODULE HashFromArrays;
IMPORT
ADT:Dictionary,
Object:Boxed;
TYPE
Key= STRING;
Value= Boxed.LongInt;
PROCEDURE Do;
VAR
a: ARRAY 128 OF Key;
b: ARRAY 128 OF Value;
hash: Dictionary.Dictionary(Key,Value);
i: INTEGER;
BEGIN
hash := NEW(Dictionary.Dictionary(Key,Value));
a[0] := "uno";
a[1] := "dos";
a[2] := "tres";
a[3] := "cuatro";
b[0] := Boxed.ParseLongInt("1");
b[1] := Boxed.ParseLongInt("2");
b[2] := Boxed.ParseLongInt("3");
b[3] := Boxed.ParseLongInt("4");
i := 0;
WHILE (i < LEN(a)) & (a[i] # NIL) DO
hash.Set(a[i],b[i]);
INC(i)
END;
END Do;
BEGIN
Do;
END HashFromArrays.

View file

@ -1,18 +1,18 @@
/*REXX program demonstrate hashing of a stemmed array (from a key). */
/*REXX program demonstrates 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*/
/* [↑] superfluous blanks added to humorous */
/* keys just because it looks prettier. */
call hash values,keys /*nothing, then let's leave Dodge*/ /*hash the keys to the values. */
parse arg query . /*get what was specified on C.L. */
if query=='' then exit /*Nothing? Then leave Dodge City*/
pad=left('',30) /*used for padding the display. */
say 'key:' query pad "value:" hash.query /*show & tell some stuff.*/
say 'key:' query pad "value:" hash.query /*show 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)
do j=1 until map==''; map=word(k,j)
hash.map=word(v,j)
end /*j*/
return

View file

@ -1,12 +1,7 @@
keys=['hal',666,[1,2,3]]
vals=['ibm','devil',123]
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
hash = Hash[keys.zip(vals)]
p hash # => {"hal"=>"ibm", 666=>"devil", [1, 2, 3]=>123}

View file

@ -0,0 +1,4 @@
keys = ['hal', 666, [1,2,3]]
vals = ['ibm', 'devil', 123]
keys.zip(vals).to_h

View file

@ -0,0 +1,2 @@
$ txr -p '^#H(() ,*[zip #(a b c) #(1 2 3)])))'
#H(() (c 3) (b 2) (a 1))

View file

@ -0,0 +1,2 @@
$ txr -p '(hash-construct nil [zip #(a b c) #(1 2 3)])))'
#H(() (c 3) (b 2) (a 1))

View file

@ -0,0 +1,7 @@
@(do
(defun hash-from-two (vec1 vec2 . hash-args)
(let ((table (hash . hash-args)))
(mapcar (do sethash table) vec1 vec2)
table))
(prinl (hash-from-two #(a b c) #(1 2 3))))

View file

@ -1,10 +0,0 @@
@(do (defun hash-from-two (vec1 vec2 equal-based-p)
(let ((table (make-hash nil nil equal-based-p)))
(for ((i 0)) ((< i (length vec1)) table) ((inc i))
(set (gethash table (vecref vec1 i) 0)
(vecref vec2 i))))))
@(bind hash @(hash-from-two #(a b c) #(1 2 3) nil))
@(bind (keys vals) @(let (k v)
(dohash (key val hash (list k v))
(push key k)
(push val v))))

View file

@ -1,4 +1,6 @@
set keys [list fred bob joe]
set keys [list fred bob joe]
set values [list barber plumber tailor]
array set arr {}
foreach a $keys b $values { set arr($a) $b }
parray arr

View file

@ -1,3 +1,10 @@
package require Tcl 8.5
set keys [list fred bob joe]
set values [list barber plumber tailor]
foreach a $keys b $values {
dict set jobs $a $b
}
puts "jobs: [dict get $jobs]"

View file

@ -0,0 +1,11 @@
keys=( foo bar baz )
values=( 123 456 789 )
declare -A hash
for (( i = 0; i < ${#keys[@]}; i++ )); do
hash["${keys[i]}"]=${values[i]}
done
for key in "${!hash[@]}"; do
printf "%s => %s\n" "$key" "${hash[$key]}"
done