associative-array

How to rename an associative array in Bash?

送分小仙女□ 提交于 2019-11-28 20:38:09
I need to loop over an associative array and drain the contents of it to a temp array (and perform some update to the value). The leftover contents of the first array should then be discarded and i want to assign the temp array to the original array variable. Sudo code: declare -A MAINARRAY declare -A TEMPARRAY ... populate ${MAINARRAY[...]} ... while something; do #Drain some values from MAINARRAY to TEMPARRAY ${TEMPARRAY["$name"]}=((${MAINARRAY["$name"]} + $somevalue)) done ... other manipulations to TEMPARRAY ... unset MAINARRAY #discard left over values that had no update declare -A

Ruby : Associative Arrays

為{幸葍}努か 提交于 2019-11-28 20:05:48
Does Ruby on rails have associative arrays? For eg: a = Array.new a["Peter"] = 32 a["Quagmire"] = 'asdas' What is the easiest method to create such an array structure 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/ newUserNameHere Use hashes, here's some examples on how to get started (all of

Are there dictionaries in php?

陌路散爱 提交于 2019-11-28 19:28:46
问题 For example: $names = {[bob:27, billy:43, sam:76]}; and then be able to reference it like this: $names[bob] 回答1: http://php.net/manual/en/language.types.array.php <?php $array = array( "foo" => "bar", "bar" => "foo", ); // as of PHP 5.4 $array = [ "foo" => "bar", "bar" => "foo", ]; ?> Standard arrays can be used that way. 回答2: No, there are no dictionaries in php. The closest thing you have is an array. However, an array is different than a dictionary in that arrays have both an index and a

Union of dict objects in Python [duplicate]

∥☆過路亽.° 提交于 2019-11-28 18:30:19
This question already has an answer here: How to merge two dictionaries in a single expression? 41 answers How do you calculate the union of two dict objects in Python, where a (key, value) pair is present in the result iff key is in either dict (unless there are duplicates)? For example, the union of {'a' : 0, 'b' : 1} and {'c' : 2} is {'a' : 0, 'b' : 1, 'c' : 2} . Preferably you can do this without modifying either input dict . Example of where this is useful: Get a dict of all variables currently in scope and their values Mechanical snail This question provides an idiom. You use one of the

Return first key of associative array in PHP

一个人想着一个人 提交于 2019-11-28 17:27:47
I'm trying to obtain the first key of an associative array, without creating a temporary variable via array_keys() or the like, to pass by reference. Unfortunately both reset() and array_shift() take the array argument by reference, so neither seem to be viable results. With PHP 5.4 I'll be in heaven; array_keys($array)[0]; , but unfortunately this of course is not an option either. I could create a function to serve the purpose, but I can only imagine there is some concoction of PHP's array_* functions that will produce the desired result in a single statement , that I cannot think of or come

How to update specific key's value in an associative array in PHP?

允我心安 提交于 2019-11-28 16:40:17
问题 I've a following associative array named $data Array ( [0] => Array ( [transaction_user_id] => 359691e27b23f8ef3f8e1c50315cd506 [transaction_no] => 19500912050218 [transaction_total_amount] => 589.00 [transaction_date] => 1335932419 [transaction_status] => cancelled ) [1] => Array ( [transaction_user_id] => 9def02e6337b888d6dbe5617a172c18d [transaction_no] => 36010512050819 [transaction_total_amount] => 79.00 [transaction_date] => 1336476696 [transaction_status] => cancelled ) [2] => Array (

Javascript: Using integer as key in associative array?

风流意气都作罢 提交于 2019-11-28 16:36:31
When I create a new javascript array, and use an integer as a key, each element of that array up to the integer is created as undefined. for example: var test = new Array(); test[2300] = 'Some string'; console.log(test); will output 2298 undefined's and one 'Some string'. How should I get javascript to use 2300 as a string instead of an integer, or how should I keep it from instanciating 2299 empty indices? Use an object, as people are saying. However, note that you can not have integer keys. JavaScript will convert the integer to a string . The following outputs 20, not undefined: var test =

Delete vs splice on associative array

那年仲夏 提交于 2019-11-28 15:43:26
If I have a JS associative array which is from what I gather is really an object, and I wish to remove an element, using delete myArr[someId] will set the element to undefined, whilst splice won't work at all... so what is the alternative for an associative array if I wish to delete an element (rather than setting it to undefined ) Geuis The terminology in js can be confusing at first, so lets straighten that out. Yes, pretty much everything in js is an object. However, there are differences in the data types. An array can be used like as associative array, but it's different than an object

How to echo element of associative array in string?

谁都会走 提交于 2019-11-28 14:19:47
I know It's a very basic question but I have to ask. I have an associative array let's say it is: $couple = array('husband' => 'Brad', 'wife' => 'Angelina'); Now, I want to print husband name in a string. There are so many ways but i want to do this way but it gives html error $string = "$couple[\'husband\'] : $couple[\'wife\'] is my wife."; Please correct me if I'm using a wrong syntax for backslash. Your syntax is correct. But, still you can prefer single quotes versus double quotes. Because, double quotes are a bit slower due to variable interpolation. (variables within double quotes are

Appending an element to associative array awk

空扰寡人 提交于 2019-11-28 12:54:14
问题 I've got an input file (input.txt) with a few fields: A1 B1 C1 D1 E1 A2 B2 C2 D1 E2 A3 B3 C3 D2 E3 A4 B4 C4 D2 E4 And I want to append elements of an associative array, awk '{a[$4]=a[$4] $5; print a[$4]} END {for(b in a) {print a[b]}}' input.txt I think the output should be (ie E2 is concatenated to E1, and E4 is concatenated to E3): E1 E2 E3 E4 but instead the output is: E2 E4 I'm not sure what's wrong with my code? 回答1: Your output isn't consistent with your command, but I assume that you