How to pass an associative array as argument to a function in Bash?

If you’re using Bash 4.3 or newer, the cleanest way is to pass the associative array by name reference and then access it inside your function using a name reference with local -n. For example: function foo { local -n data_ref=$1 echo ${data_ref[a]} ${data_ref[b]} } declare -A data data[a]=”Fred Flintstone” data[b]=”Barney Rubble” foo data You … Read more

Merge two numerically-keyed associative arrays and preserve the original keys

Just use: $output = array_merge($array1, $array2); That should solve it. Because you use string keys if one key occurs more than one time (like ’44’ in your example) one key will overwrite preceding ones with the same name. Because in your case they both have the same value anyway it doesn’t matter and it will … Read more

Custom key-sort a flat associative based on another array

Just use array_merge or array_replace. array_merge works by starting with the array you give it (in the proper order) and overwriting/adding the keys with data from your actual array: $customer[‘address’] = ‘123 fake st’; $customer[‘name’] = ‘Tim’; $customer[‘dob’] = ’12/08/1986′; $customer[‘dontSortMe’] = ‘this value doesnt need to be sorted’; $properOrderedArray = array_merge(array_flip(array(‘name’, ‘dob’, ‘address’)), $customer); … Read more

Associative Arrays in Ruby

Unlike PHP which conflates arrays and hashes, in Ruby (and practically every other language) they’re a separate thing. http://ruby-doc.org/core/classes/Hash.html In your case it’d be: a = {‘Peter’ => 32, ‘Quagmire’ => ‘asdas’} There are several freely available introductory books on ruby and online simulators etc. http://www.ruby-doc.org/