Author Topic: Value in array  (Read 6229 times)

0 Members and 1 Guest are viewing this topic.

Offline Cloudee1

Value in array
« 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
   )
)
« Last Edit: July 24, 2014, 11:39:24 PM by Cloudee1 »


Halloween Competition Brass Lantern Prop Competition Groundhog Day Competition

Offline Collector

Re: Value in array
« Reply #1 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?
KQII Remake Pic

Offline gumby

Re: Value in array
« Reply #2 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
)
« Last Edit: July 24, 2014, 07:07:02 PM by gumby »
In the Great Underground Empire (Zork port in development)
Winter Break 2012 Rope Prop Competition

Offline Collector

Re: Value in array
« Reply #3 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.
KQII Remake Pic

Offline Cloudee1

Re: Value in array
« Reply #4 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.
« Last Edit: July 25, 2014, 12:40:09 AM by Cloudee1 »
Halloween Competition Brass Lantern Prop Competition Groundhog Day Competition

Offline Collector

Re: Value in array
« Reply #5 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.
KQII Remake Pic


SMF 2.0.19 | SMF © 2021, Simple Machines
Simple Audio Video Embedder

Page created in 0.021 seconds with 15 queries.