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.
Related
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]}
Say I have an array of strings: var myFavouriteSites = ["www.fb.com", "www.xe.com", "www.youtu.be"];
Is there a way I can push this array into the realtime database as one 'field'? All stored in one place? Because I have read that they must be matched with a unique id/key and I am not familiar of how to do it.
I'm not sure what you mean by "in one place". But I can tell you that everything in Realtime Database is essentially key/value pairs, where the values are either strings, numbers, or other objects. There is no native "array" value type. Arrays get translated into objects where the keys are array indices. So if you assign an array at /location:
["www.fb.com", "www.xe.com", "www.youtu.be"]
You'll get a database structure that looks like this:
/location
0: "www.fb.com"
1: "www.xe.com"
2: "www.youtu.be"
How can I write a NSPredicate that will check if all the elements in an NSArray are the same? I need this for a more complex predicate that I am using to filter NSTableView.
NSPredicate can't tell you if all the items are the same, but you could use it to filter an array for objects that are not equal to object[0]. This wouldn't be particularly performant since it would have to check the entire contents of the array.
You could instead use - (void)enumerateObjectsWithOptions: then bailing out when you find a different object via *stop = YES.
Consider looking at using NSSet, since that is designed to store a collection of unique objects (albeit unordered).
Without knowing more detail it's impossible to say what is the best solution for you.
i knew its a weird Idea to think about, i want to know if it is possible to reverse the mechanism of sorting, (i dont want to reverse the order).
for example lets say i have a random array of integers, then i sorted the array with quicksort method, now i want to go back and un-sort the array and get it back to the it was.
you may suggest i save a copy of the array, thats not what i want, think of it as a time line and have the ability to go backward or forward of sorting method.
and if it is possible please consider showing me the best way to do it with Delphi XE.
Thanks in advance.
It is not possible to unsort. You have to either:
create a separate array that holds a copy of the values and then sort that array so that you can preserve the original array.
create a separate array that holds pointers/indexes to the values in the original array, and then sort the second array using the values it refers to.
I usually use a function that given an array returns an array of the sorted indexes.
This way you will always have the original data and you will be able to access the data in a sorted way using something like:
for jIndex in ASortedIndexesArray do
ShowMessage(AOriginalArray[jIndex]);
Hope this helps.
Mirko
If you want to think of it as a time line and have the ability to go backward or forward of sorting method then organize it like a timeline - with records in a file. Save each step and you will be able to reproduce it.
If the array is of integers the indices will not help you as one index (a pointer) takes the same memory as one array element. If you lack RAM use a file to store and retrieve the array. If you use larger data structures you can create, store and retrieve indices as David suggested.
You could log the swapping actions that quicksort (or whatever sorting algorithm you use) does to a list and then go forward and backward in that list to undo/redo these actions. Not simple to implement, but doable.
many people use extensively arrays in Excel/VBA to store a list of data. However, there is the collection object which in my view is MUCH MUCH more convenient (mainly: don't need to re/define length of the list).
So, I am sincerely asking myself if I am missing something? Why do other people still use arrays to store a list of data? Is it simply a hangover of the past?
Several reasons to use arrays instead of collections (or dictionaries):
you can transfer easily array to range (and vice-versa) with Range("A1:B12") = MyArray
collections can store only unique keys whereas arrays can store any value
collections have to store a couple (key, value) whereas you can store whatever in an array
See Chip Pearson's article about arrays for a better understanding
A better question would rather be why people would use collections over dictionaries (ok, collections are standard VBA whereas you have to import dictionaries)
#CharlesWilliams answer is correct: looping through all the values of an array is faster than iterating a Collection or dictionary: so much so, that I always use the Keys() or Items() method of a dictionary when I need to do that - both methods return a vector array.
A note: I use the Dictionary class far more than I use collections, the Exists() method is just too useful.
There are, or course, drawbacks to collections and dictionaries. One of them is that arrays can be 2- or even 3-Dimensional - a much better data structure for tabulated data. You can store arrays as members of a collection, but there's some downsides to that: one of them is that you might not be getting a reference to the item - unless you use arrItem = MyDictionary(strKey) you will almost certainly get a 'ByVal' copy of the array; that's bad if your data is dynamic, and subject to change by multiple processes. It's also slow: lots of allocation and deallocation.
Worst of all, I don't quite trust VBA to deallocate the memory if I have a collection or dictionary with arrays (or objects!) as members: not on out-of-scope, not by Set objCollection = Nothing, not even by objDictionary.RemoveAll - it's difficult to prove that the problem exists with the limited testing toolkit available in the VBE, but I've seen enough memory leaks in applications that used arrays in dictionaries to know that you need to be cautious. That being said, I never use an array without an Erase command somewhere.
#JMax has explained the other big plus for arrays: you can populate an array in a single 'hit' to the worksheet, and write back your work in a single 'hit.
You can, of course, get the best of both worlds by constructing an Indexed Array class: a 2-dimensional array with associated collection or dictionary objects storing some kind of row identifier as the keys, and the row ordinals as the data items.
Collections that auto-resize are slower (theoretically speaking, different implementations will obviously have their own mileage). If you know you have a set number of entries and you only need to access them in a linear fashion then a traditional array is the correct approach.