Home   Help Search Login Register  

Author Topic: Retrieving an array index?  (Read 1938 times)

0 Members and 1 Guest are viewing this topic.

Offline Mr.Peanut

  • Former Staff
  • ****
  • urp!
Retrieving an array index?
« on: 23 Nov 2004, 23:17:40 »
Is it possible to retrieve the index of an item within an array?
urp!

Offline dmakatra

  • Members
  • *
  • Better known as Armsty
Re:Retrieving an array index?
« Reply #1 on: 23 Nov 2004, 23:23:05 »
There's a tute about that. Check ed depot, look for "variables and arrays in OFP" or something similar.

:beat: *Gets Shot* :beat:

Offline Mr.Peanut

  • Former Staff
  • ****
  • urp!
Re:Retrieving an array index?
« Reply #2 on: 23 Nov 2004, 23:39:05 »
No, the array tut covers everything but my question.  I want to know if there is any way to retrieve the position of an item in an array without having to search the whole pissing array element by element.
 
If I can do this:

MyArray set [3, "OPFL array handling is crap"]

Why is there not a corresponding "get" command that will return an array index?

e.g.
Index = get [MyArray,"OPFL array handling is crap"]


There's a tute about that. Check ed depot, look for "variables and arrays in OFP" or something similar.

:beat: *Gets Shot* :beat:

urp!

Offline dmakatra

  • Members
  • *
  • Better known as Armsty
Re:Retrieving an array index?
« Reply #3 on: 23 Nov 2004, 23:56:28 »
isn't there a in command? Like

? ("OPFL array handling is cool" in array1) : hint "It's in the tute, I think."

:beat: *Gets Shot* :beat:

Offline Mr.Peanut

  • Former Staff
  • ****
  • urp!
Re:Retrieving an array index?
« Reply #4 on: 24 Nov 2004, 00:01:50 »
the "in" command simply returns true or false...
urp!

Offline Blanco

  • Former Staff
  • ****
« Last Edit: 24 Nov 2004, 00:28:27 by Blanco »
Search or search or search before you ask.

Offline Mr.Peanut

  • Former Staff
  • ****
  • urp!
Re:Retrieving an array index?
« Reply #6 on: 24 Nov 2004, 00:32:23 »
Thanks for the function link, but this was what I was trying to avoid, having to search the whole array for each element.  I guess it can not be done in a more straight forward way.
urp!

Offline macguba

  • Former Staff
  • ****
    • macguba's operation flashpoint page
Re:Retrieving an array index?
« Reply #7 on: 24 Nov 2004, 02:19:56 »
This is an interesting one.   Reading the original question, I know for a certain fact that the answer is yes.    However, I have no idea whatsoever of how to do it.    Regular readers of my posts (if any) will appreciate that this summarises my scripting non-skill beautifully.

It is frequently the case that OFP code will do what you want, but is a little inelegant.   In such cases it is necessary to make a judgement call as to whether you can stomach the inelegance or not.   Not, of course, means abandoning the whole misssion/addon/idea.

It would seem, in this case, that your options are to

1) abandon the whole thing

2) go with GB's function (he's a good lad, it probably works)

3) invent your own better, faster, sexier, cooler function
Plenty of reviewed ArmA missions for you to play

Korax

  • Guest
Re:Retrieving an array index?
« Reply #8 on: 24 Nov 2004, 03:21:39 »
Or, get spend $200 and get VBS, it has a 'find' command which is exactly what you want :P

Offline THobson

  • OFPEC Patron
  • Former Staff
  • ****
Re:Retrieving an array index?
« Reply #9 on: 24 Nov 2004, 07:56:08 »
Excsue my ignorance, but what is VBS?

Offline LCD

  • Former Staff
  • ****
    • Everon Cartel
Re:Retrieving an array index?
« Reply #10 on: 24 Nov 2004, 08:11:18 »
*points @ THobson and laughs

u dont know whats VBS :wow:

its OFP military version thats all ::)

LCD OUT
"guess being the community has downsides .." - cheetah
Help Perfecting Da Next Best Thing - O-Team Beta

Offline THobson

  • OFPEC Patron
  • Former Staff
  • ****
Re:Retrieving an array index?
« Reply #11 on: 24 Nov 2004, 15:18:58 »
LCD - are you a lawyer?

You answered the question, the answer was complete, accurate and totally useless to me.  Well actually not quite.   ;) So apart from googling VBS what can I do to find out more?
« Last Edit: 24 Nov 2004, 15:19:51 by THobson »

Offline dmakatra

  • Members
  • *
  • Better known as Armsty
Re:Retrieving an array index?
« Reply #12 on: 24 Nov 2004, 15:23:17 »
VBS = OFP with better graphics and 100 times more expensive.

Kinda like art. Except the other way round. The uglier it gets, the more it costs.

:beat: *Gets Shot* :beat:

Offline THobson

  • OFPEC Patron
  • Former Staff
  • ****
Re:Retrieving an array index?
« Reply #13 on: 24 Nov 2004, 15:50:41 »
dmpkatra:  I detect a value judgement creeping in.

Your damned banana give me real problems at work - I just had someone commenting on what serious work I was involved with!!

Offline dmakatra

  • Members
  • *
  • Better known as Armsty
Re:Retrieving an array index?
« Reply #14 on: 24 Nov 2004, 16:03:22 »
ROFL. I have actually been thinking of changing it. But I thought a name change was dramatic enough. If I would take away OFPECs favorite banana all hell would break loose. ;D

:beat: *Gets Shot* :beat:

Offline THobson

  • OFPEC Patron
  • Former Staff
  • ****
Re:Retrieving an array index?
« Reply #15 on: 24 Nov 2004, 16:09:03 »
I agree - keep the banana

Offline MachoMan

  • Honoured
  • Former Staff
  • ****
  • KISS, Keep it Simple Stupid
Re:Retrieving an array index?
« Reply #16 on: 24 Nov 2004, 16:09:09 »
I made a better one  ;D

In a worst case scenario it is 2x as fast as GB's

Code: [Select]
/*       MachoMan's
          Inspired
             by
             GB
         ==========
    Superior Index Finder
      Works 2x as Fast!! ;)
         ==========

  call: [item, array] call index
  Returns an index of the item.
  if not in Array return -1.
*/

private ["_array","_item","_index1","_index2","_found1","_found2"];

_item = _this select 0;
_array = _this select 1;
_found1 = FALSE;
_found2 = FALSE;
_index1 = 0;
_index2 = count _array -1;

while { !(_found1 OR _found2) AND (_index1 <= _index2) } do
{
   if (_item1 == (_array select _index1) AND (_found1 = FALSE)) then {_found1 = TRUE} else {_index1 = _index1 + 1};
   if (_item2 == (_array select _index2) AND (_found2 = FALSE)) then {_found2 = TRUE} else {_index2 = _index2 - 1};
};

if (found2) then {_index1 = _index2);
if (!(_found1 OR _found2)) then {_index1 = -1};

_index1

Most Computer Science ppl will know this algorithm ;)

(Syntax may contain minor errors)
Get those missions out there you morons!

Offline Mr.Peanut

  • Former Staff
  • ****
  • urp!
Re:Retrieving an array index?
« Reply #17 on: 24 Nov 2004, 17:57:32 »
Is the above algorithm faster for all sizes of arrays? Please tell the name of the  search algorithm your code is based upon e.g. binary?

Never mind. I see now that it simply searches the array from both ends.
« Last Edit: 24 Nov 2004, 18:32:05 by Mr.Peanut »
urp!

Offline MachoMan

  • Honoured
  • Former Staff
  • ****
  • KISS, Keep it Simple Stupid
Re:Retrieving an array index?
« Reply #18 on: 24 Nov 2004, 19:41:19 »
Lol implementing binary search in ofp would be simple insane, perhaps I should try try it ;D

But binary search unfortunately only works on sorted lists of integers (numbers).

So yes it simply searches the array both ways, because loops are inherently slow.
Get those missions out there you morons!

Offline CrashDome

  • Members
  • *
Re:Retrieving an array index?
« Reply #19 on: 25 Nov 2004, 02:16:36 »
Instead of returning a -1 after checking array, you can check the array via the 'in' command before your search. This would eliminate the searching if it isn't in the array.

i.e. stuff your search into a  

if (_item in _array) then
{
  ; all your stuff
}
else
{_index1 = -1;};

_index1

Offline MachoMan

  • Honoured
  • Former Staff
  • ****
  • KISS, Keep it Simple Stupid
Re:Retrieving an array index?
« Reply #20 on: 25 Nov 2004, 09:30:37 »
True, but I think in will go through the entire array from the beginning to the end. So this will take longer, because we have to go through it anyway. If we use in we will loop the array 2x.
Get those missions out there you morons!

Offline CrashDome

  • Members
  • *
Re:Retrieving an array index?
« Reply #21 on: 25 Nov 2004, 19:30:55 »
*doh!*

Unnamed

  • Guest
Re:Retrieving an array index?
« Reply #22 on: 25 Nov 2004, 19:41:03 »
I work on the assumption that any internal OFP script command will work faster than it's scripted equivelant. So the in command should be quicker at checking an array. But as your also going to be searching the array you may as well do them all in one script.

Unless your going to search through hundreds of array elements, a basic forEach loop should be quick enough:

Code: [Select]
_Found=False
_Index=0
{If !(_Found) Then {If (_x==_ThingToFind) Then {_Found=True} Else {_Index=_Index+1}}} ForEach _Array

BTW If you want to find an object in an array of hundreds\thousands, you can avoid searching loops altogether.

What is it exactly, your going to be searching for?

Offline MachoMan

  • Honoured
  • Former Staff
  • ****
  • KISS, Keep it Simple Stupid
Re:Retrieving an array index?
« Reply #23 on: 25 Nov 2004, 21:35:26 »
Noname, your script is cool, but it uses one flawed assumption:
There is no guaranty that foreach will start at element zero.

Perhaps it does, but we can never know it for sure, can we? ;) The comref doesn't say it, so BIS doesn't guaranty it. (Not that it does guaranty the functioning of any other command. ;D )
« Last Edit: 25 Nov 2004, 21:36:17 by MachoMan »
Get those missions out there you morons!

Komuna

  • Guest
Re:Retrieving an array index?
« Reply #24 on: 26 Nov 2004, 12:01:59 »
Actually, MachoMan, it does.

Check v.1073/1.074 - the new scripting structure is basically made of arrays, and you may notice one or two scripts using this method to either search for values or call commands through a certain order.

Unnamed

  • Guest
Re:Retrieving an array index?
« Reply #25 on: 26 Nov 2004, 12:45:40 »
Quote
Perhaps it does, but we can never know it for sure, can we?

I must admit, it would not suprise me :) But I use it quite alot for small arrays and Ive had no problems yet. At a push just change it to:

Code: [Select]
{If !(_Found) Then {If ((_Array Select _Index)==_ThingToFind) Then {_Found=True} Else {_Index=_Index+1}}} ForEach _Array
It is just a basic search, the script you posted should return 90% of results quicker, I use the same method  to range in Mortars.

But thats essentialy what the find command will do, although I rekon you would have to be searching a pretty big array to notice the speed difference between VBS's find command and any of these scripts.
« Last Edit: 26 Nov 2004, 12:46:19 by Unnamed »

Offline LCD

  • Former Staff
  • ****
    • Everon Cartel
Re:Retrieving an array index?
« Reply #26 on: 26 Nov 2004, 14:16:04 »
some1 found me a job :wow: now give me lawyers office thatll gimme work :P

LCD OUT
"guess being the community has downsides .." - cheetah
Help Perfecting Da Next Best Thing - O-Team Beta