Community

SCI Programming => SCI Syntax Help => Topic started by: Cloudee1 on July 24, 2014, 05:11:30 PM

Title: Value in array
Post by: Cloudee1 on July 24, 2014, 05:11:30 PM
So I have an array of integers.
 
Code: [Select]
specialCase[5] = (5 9 20 21 22)
I also have a for loop later on
 
Code: [Select]
(for (= i 1) (<= i totalCount) (++i)
Does anyone know the syntax for checking to see if a value is stored in the array? Such that I am trying to do something like:
Code: [Select]
(for (= i 1) (<= i totalCount) (++i)
   (if(not(inArray i specialCase)) // Here's where my question lies
   doSomething
   )

   (else // this would be for the values in the array
   doSomethingElse
   )
)
Title: Re: Value in array
Post by: Collector on July 24, 2014, 05:53:34 PM
What about just putting in a print statement at "doSomething" and "doSomethingElse" (with a unique string added on to ID which one is being called) to display what value is being held at that point?
Title: Re: Value in array
Post by: gumby on July 24, 2014, 06:59:41 PM
This should work.  Basically, just loop through the array looking for your value.  Once you find it, set a flag.  After you are done looping, check to see if the flag is unset to handle the scenario where it isn't in the array

Code: [Select]
= foundFlag FALSE

(for (= i 1) (<= i totalCount) (++i)
   (if(== specialCase[i] anotherSpecialCase)
       doSomething   // it's in the array
       = foundFlag TRUE   // indicate that we found it
   )
)

(if (== foundFlag FALSE)
       doSomethingElse   // it's not in the array
)
Title: Re: Value in array
Post by: Collector on July 24, 2014, 08:40:02 PM
That would be better. My way would bring up a message for every loop, whether it was the right value or not.
Title: Re: Value in array
Post by: Cloudee1 on July 24, 2014, 11:54:29 PM
Gumby, that is pretty much what I did. Just for a little explanation in case someone else comes across this....

First there is a for loop which iterates 1 through whatever value totalCount is set to... in my case it is 34. Each time through the loop, a local variable isSpecial is set to FALSE. Then a second (nested) for loop is triggered which loops through my specialCase array. In this case, there are currently 7 values stored in may array. Hence the reason I am telling it to loop 7 times. If at any point, the value stored in a specialCase slot matches the i (number of current iterations of outside for loop) then set the isSpecial to TRUE, otherwise it stays set at FALSE. So... now I know if the current value of i is in the Array of specialCase.

Code: [Select]
totalCount = 34
specialCase[7] = (4 5 15 19 20 23 26)

...

(for (= i 1) (<= i totalCount) (++i)

     = isSpecial FALSE // resets at each iteration of outer loop

     (for (= a 0) (< a 7) (++a)
         (if(== specialCase[a] i)
         = isSpecial TRUE
   )
     )// end special for loop


    (if(not(isSpecial))
              // do Something
    )
    (else
             // do something else
    )
)// end main for loop

Just to share some code, here is what happens in my script if it is not special... which is currently 27 times

Code: [Select]
  (if(not(isSpecial))
  (if((> (send pEvent:x)(send thisItem:nsLeft))and // item ###
      (< (send pEvent:x)(send thisItem:nsRight))and
      (> (send pEvent:y)(send thisItem:nsTop))and
      (< (send pEvent:y)(send thisItem:nsBottom)))
      (switch(send thisItem:cel)
      (case 0 )// cel 0, probly been dropped
      (case i  // its there and waiting
          (switch(gCurrentCursor)
            (case 998 Print(500 i #title thisTitle #icon 950 4 i #time 15 ))// look
            (case 995 SetCursor(i HaveMouse())// get
                      = itemIcon i
                      = gCurrentCursor i)
            (case i Print(501 0)) // itself               
            (default Print(501 10)) // other items
          )// end current cursor switch
)// end cel case
      )// end inventory cel switch
  )// ends click on view
  )// ends not special...

So now in my pnc inventory, I only need to add the click interactions for the 7 special ones, and not the other 27 items. Considering this to be a good day.
Title: Re: Value in array
Post by: Collector on July 25, 2014, 12:51:46 PM
I misunderstood. I thought you were trying to troubleshoot your code, not write working code. Not sure why I made that assumption.