associative

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

眉间皱痕 提交于 2019-11-27 07:25:18
How do you pass an associative array as an argument to a function? Is this possible in Bash? The code below is not working as expected: function iterateArray { local ADATA="${@}" # associative array for key in "${!ADATA[@]}" do echo "key - ${key}" echo "value: ${ADATA[$key]}" done } Passing associative arrays to a function like normal arrays does not work: iterateArray "$A_DATA" or iterateArray "$A_DATA[@]" Florian Feldhaus I had exactly the same problem last week and thought about it for quite a while. It seems, that associative arrays can't be serialized or copied. There's a good Bash FAQ

CSV to Associative Array

六眼飞鱼酱① 提交于 2019-11-26 22:07:58
I've seen numerous examples on how to take a CSV file and then create an associative array with the headers as the keys. For example: Brand,Model,Part,Test Honda,Civic,123,244 Honda,Civic,135,434 Toyota,Supra,511,664 Where it would create an Array such as Array[$num][$key] where $key would be Brand, Model, Part, Test. So If I wanted to access the test value "434" I would have to loop every index in the array and then ignore any Brands that were not honda, and any models that were not Civic What I need to do is access the value most directly, instead of running through a for loop going through

How to make a right-associative infix operator?

大兔子大兔子 提交于 2019-11-26 20:07:53
问题 I have an associative operation >> . The problem is that its cost linearly depends on the size of its left operand. So an expression formed by a sequence of n applications of >> like a >> a >> a >> a >> a >> ... >> a it has quadratic cost in terms of n , because by default infix operators are left-associative. How to make it right-associative so that the cost of such an expression is kept linear in terms of n ? 回答1: I found a solution. Scala reference says in section 6.12.3 Infix Operations :

Is array both associative and indexed?

≯℡__Kan透↙ 提交于 2019-11-26 17:37:57
Can an array in JavaScript be associative AND indexed? I'd like to be able to lookup an item in the array by its position or a key value. There are no such things as associative arrays in Javascript. You can use object literals, which look like associative arrays, but they have unordered properties. Regular Javascript arrays are based on integer indexes, and can't be associative. For example, with this object: var params = { foo: 1, bar: 0, other: 2 }; You can access properties from the object, for example: params["foo"]; And you can also iterate over the object using the for...in statement:

How to sort an associative array by its values in Javascript?

青春壹個敷衍的年華 提交于 2019-11-26 12:53:21
I have the associative array: array["sub2"] = 1; array["sub0"] = -1; array["sub1"] = 0; array["sub3"] = 1; array["sub4"] = 0; What is the most elegant way to sort (descending) by its values where the result would be an array with the respective indices in this order: sub2, sub3, sub1, sub4, sub0 ? Ben Blank Javascript doesn't have "associative arrays" the way you're thinking of them. Instead, you simply have the ability to set object properties using array-like syntax (as in your example), plus the ability to iterate over an object's properties. The upshot of this is that there is no guarantee

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

你。 提交于 2019-11-26 10:57:01
问题 How do you pass an associative array as an argument to a function? Is this possible in Bash? The code below is not working as expected: function iterateArray { local ADATA=\"${@}\" # associative array for key in \"${!ADATA[@]}\" do echo \"key - ${key}\" echo \"value: ${ADATA[$key]}\" done } Passing associative arrays to a function like normal arrays does not work: iterateArray \"$A_DATA\" or iterateArray \"$A_DATA[@]\" 回答1: I had exactly the same problem last week and thought about it for

Is array both associative and indexed?

你说的曾经没有我的故事 提交于 2019-11-26 06:06:42
问题 Can an array in JavaScript be associative AND indexed? I\'d like to be able to lookup an item in the array by its position or a key value. 回答1: There are no such things as associative arrays in Javascript. You can use object literals, which look like associative arrays, but they have unordered properties. Regular Javascript arrays are based on integer indexes, and can't be associative. For example, with this object: var params = { foo: 1, bar: 0, other: 2 }; You can access properties from the

How do I remove objects from a javascript associative array?

人走茶凉 提交于 2019-11-26 04:58:01
问题 Suppose I have this code: var myArray = new Object(); myArray[\"firstname\"] = \"Bob\"; myArray[\"lastname\"] = \"Smith\"; myArray[\"age\"] = 25; Now if I wanted to remove \"lastname\"?....is there some equivalent of myArray[\"lastname\"].remove() ? (I need the element gone because the number of elements is important and I want to keep things clean.) 回答1: Use the "delete" keyword in Javascript. delete myArray["lastname"]; EDIT: In some JavaScript engine, the delete keyword might hurt

How to sort an associative array by its values in Javascript?

吃可爱长大的小学妹 提交于 2019-11-26 03:07:28
问题 I have the associative array: array[\"sub2\"] = 1; array[\"sub0\"] = -1; array[\"sub1\"] = 0; array[\"sub3\"] = 1; array[\"sub4\"] = 0; What is the most elegant way to sort (descending) by its values where the result would be an array with the respective indices in this order: sub2, sub3, sub1, sub4, sub0 ? 回答1: Javascript doesn't have "associative arrays" the way you're thinking of them. Instead, you simply have the ability to set object properties using array-like syntax (as in your example