• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

is it possible to load the value of an array's index

Status
Not open for further replies.
Level 8
Joined
Jan 8, 2010
Messages
493
i ought to ask this sooner. but there, is it possible?
and it's not something like:

Set Key = Some Value
Set Variable[key] = Some Value

therefore the value of the index is Key, but rather

...Variable[Key] = Some Value
Set AnotherVariable = Key

if it's possible with either GUI or JASS, just show me how. (i'll try my best to understand if it's in JASS)
 
Level 8
Joined
Jan 8, 2010
Messages
493
wait, i mean, like the example. when i set a variable VariableArray[43], how do i load/get that "43" without setting another variable=43?

i know very well that the following sets the value of the array's index to 43.

  • Set A=43
  • Set B[A]=3
but how will i do the following?

  • Set B[43]=3
  • Set A=index of B
 
Let's say integer parameter 'IndexSize' stores the maximum array index.
Now, just loop through the indexes, and stop after finding the correct one:

  • Set B[43] = 3
  • For each i from 1 to IndexSize do Actions
    • Loop - Actions
      • Custom script: if udg_B[udg_i] == 3 then
      • Set A = i
      • // actions here, index has been found
      • Custom script: return
      • Custom script: endif
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,258
but how will i do the following?
Set B[43]=3
Set A=index of B
This is total nonsense. How on earth would a computer know which index of B you are trying to refer to...

Maybe in languages designed for parallel processing this would be possible as you would map the array of B to A and then perform on every index the opperations done to A but certainly this is not possible in JASS.

If you want to find the index of a specific value in an array list then you will need to use a search algorthim. Otherwise just use the first method (store the index to a variable and then use that variable as the index and in further opperations).
 
Level 8
Joined
Jan 8, 2010
Messages
493
@Dr Super Good
yup, that's exactly what i asked. how can i (or the computer) get the index of B that a certain action would refer to, if it's possible or not (right now i guess doing the rundown like Spinnaker said would be the best thing to do)

@Maker
it's not really for an elaborate system or so, but i was thinking like if i set certain units to Units[Index], and i want to do something to Units[3] only. that's why i want to know how to get that index "3", or if it's possible. oh, or i think a much better example would be (but i haven't done this) when in a trigger i pick a certain unit from Units[Index] and i want to know what Index that unit is.
 
Level 8
Joined
Jan 8, 2010
Messages
493
hehe yup, i was wrong there. even my first example was kind of.. wrong since i already said that the index would be "3" XD (referring to my reply on Maker)
but just like my second example said. if i don't know what index that unit's Units[Index] has and i want to get the value of Index.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,258
What you are asking (apparently) is...
How do I find the index of a certain value in a collection?

You have many options. You could use a hashtable system to map values to indicies ontop of the array. You could use another array to do it if the mapped values are small in range (below 2^13).

These are practicle solutions to avoid even searching. There are dozens of search routeines you can use such as linear search or binary search (if collection is ordered). You could even use a tree structure for more efficient searches.
 
Level 8
Joined
Jan 8, 2010
Messages
493
that probably best describes what i wanted to ask! and "collection" would be the arrayed variable?

and the index would not be that great. it's mostly only for units and just planning for locations, so it wouldn't reach half of 2^13 (it's 8192 right? the max value?)

and i don't understand linear or binary searches. (what kind of search is Spinnaker's suggestion?) is there a tutorial here?
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,258
that probably best describes what i wanted to ask! and "collection" would be the arrayed variable?
I made a mistake. A "Set" best describes what you are doing as I assume you do not give 2 indicies the same value (which is a property of a Set but not of a Collection).

Although such things are implimented in script using arrays ultimatly, the algorthims and procedures used might only accept a certain type of data.

For example, how can you logically return the index of a stored value when the same value has more than 1 index? This is capable of occuring when data is stored in a collection but not when stored in a set (as each value must be unique).

Additionally, you need to define a size for the set. Using a search algorthim has a runtime computational cost proportional to the size of the data pool being searched so itterating through all 2^13 indicies is not fesible. If you only have 1 element stored then you should not need to search at all, and with 2 elements it should itterate only once.

If you store your data in the array in a discontinious way (eg, with blank indicies between stored values) then this will potentially slow the search down (wasted itterations). Obviously you can assign a NO_VALUE constant to tell the search to ignore indicies with no value but this is still not as effective as using a set in the first place.

You might also whish to more directly map an index to a value using a hasthable (or Array if values are in a small range). This is far faster than searching but requires you to keep the 2 mappings syncronized (the index:value and value:index mappings are stored separatly and need to maintain integrity for the system to work bug free).

In eithor way you probably want a set of values.
 
Status
Not open for further replies.
Top