I'm a beginner to bash, and I'm quite struggling with associative array. I created many associative arrays and integrated all the data by using key '0, 1, 2, 3'. Simply saying ArrayA[0], ArrayB[0], ArrayC[0].... have data on a common topic. So these keys are important to me. Now I want to make another array to access these keys, and I want the keys of the new arrays to be the data of the related keys. For example,
declare -A NewArray
ArrayA[0]=3
ArrayB[0]=4
ArrayC[0]=6
NewArray[ArrayA[0]+ArrayB[0]+ArrayC[0]]=0
so that it could be NewArray[346]=0 or NewArray[3,4,6]=0. Any helps would be so much appreciated, I've been thinking about this for hours.
Bash has no concatenation operator as it doesn't need any.
NewArray[${ArrayA[0]}${ArrayB[0]}${ArrayC[0]}]=0
echo ${NewArray[346]}
or
NewArray[${ArrayA[0]},${ArrayB[0]},${ArrayC[0]}]=0
echo ${NewArray[3,4,6]}
Related
I'm looking for any solutions to keep data by using associative array.
Now i found only way to do is just keep in array. Any for suggestion.
example the result that i want is something like basic concept of associative array
as we know... it had key and value.
I'm a beginner (a wet lab biologist, who has to fiddle a bit with bioinformatics for the first time in my life) and today I've got stuck on one problem: how to parse an array to a hash of arrays in perl?
This doesn't work:
#myhash{$key} = #mytable;
I've finally circumvented my problem with a for loop:
for(my $i=0;$i<=$#mytable;$i++){$myhash{$key}[$i]=$mytable[$i]};
Of course it works and it does what I need to be done, but it seems to me not a solution to my problem, but just a way to circumvent it... When something doesn't work I like to understand why...
Thank you very much for your advice!
If you are asking how to put an array as one value of a hash, you do this by taking a reference to the array, since references are scalars and the values of hashes must be scalars. This is done with the backslash operator.
$myhash{$key} = \#mytable;
The for loop you describe creates such a reference through autovivification, as $myhash{$key}[0] creates an array reference at $myhash{$key} in order to assign to its index. Also note that the difference between taking a reference and copying each value is that in the former case, changes to the array after the fact will also affect the values referenced via the hash value, and vice versa.
$mytable[5] = 42; # $myhash{$key}[5] is also changed
As Grinnz mentioned you can save a reference to an array, but any change on the array latter will be reflected in hash (it is same data).
For example if you reuse same array in the loop then data in hash will reflect last iteration of the loop.
In such case you will want a copy of array stored in the hash.
#{$hash{$key}} = #array;
Programming Perl: Data strutures
I am currently making my first noteworthy awk script (specifically, gawk). Naturally, there were many questions, so I read most of its manual and stumbled across the following statement:
awk’s arrays are efficient—the time to access an element is
independent of the number of elements in the array.
How does gawk achieve this, given that its arrays are associative and the array indices are strings?
Even if they are hashing the actual index values, those hashes still would have to be stored in appropriate data structures like binary trees etc., where the access time is not independent of the number of elements.
I could read the source code trying to understand how it is done, but I doubt that I would completely understand this in a reasonable time. So I am hoping that somebody could shortly explain the underlying technique or provide a reference.
Since Tcl 8.5, we have both dictionaries, and arrays. Now, everybody knows of the advantages of the dictionaries.
Is there an advantage to an array, other than the environment array?
Has anyone found the arrays' advantage, assuming that one needs not use the TCL older than 8.5?
You can trace an array variable, but you cannot trace a dictionary value.
Other than that, the syntax for fetching an array value is more terse.
References: array dict
The big semantic advantage of arrays is that you can trace elements of the array; they really are collections of variables. This also means that you can use elements with commands like vwait, and have Tk widgets use them to store their models, and so on. (All of those depend on traces to work.)
The big semantic advantage of dictionaries is that you can pass them from one context to another cheaply; they really are values. This makes using them as an argument to a procedure or returning it from a procedure both trivial and cheap.
Syntactically, arrays are nicer.
What is the proper term for the type of array that does not contain textual keys?
That is to say
$my_array[0], $my_array[1] etc. vs $my_array['some-key']
An 'indexed' array? Is there even such a term for this type?
It is simply an array, the associative array you are referring to is a language implementation that will use a different underlying data structure and is not even possible in C. By definition when you are referring to an array by index that is all it is.
In computer science, an array data structure or simply array is a data
structure consisting of a collection of elements (values or
variables), each identified by at least one index.
-Wikipedia
Numerical? Numerically Indexed? I don't think there's an agreed upon term. When you retrieve a php array it can be "ASSOC" for associative or "NUM" for numerical. So that's what I'm guess, someone should know what you mean if you used either of those.
a numerically indexed array: here
I think it's just an array, but w3schools.com uses the term "Numeric Arrays", which would clear up any ambiguity between the two types.