none
Help in checking to see if a variable has been used already RRS feed

  • Question

  • I'm writting a program that uses the textwindow.read object to return the name of a variable; ItemNo
    Say ItemNo could be used up to 50 times,
    What would be a good approach to see if the textwindow return value was previously used?
    So when the value of ItemNo is read, I can check to see if it has been used already.
    Tuesday, June 2, 2009 8:16 PM

Answers

  • An array holds a set of related variables in a list such as a list of names.

    A array has a name which is a string - a label in quotes such as "Names"

    Each entry in the array has an index, usually a number running from 1, 2, 3 etc.

    So the array is a list with name and indexes to the items in the array (values).

    So an array called "Names" could include "John", "Fred" and "Marry", with indexes 1, 2 and 3.

    Index  Value
    1        "John"
    2        "Fred"
    3        "Mary"

    The indexes do not have to be sequential numbers, but they often are.
    The values can be of any type , numbers, strings, images, graphic objects etc - so we could hold for example an array of images in game.

    Look at the intellisense for Array to see the options to add, remove, check if the array contains a variable or index, get the number of variables in the array etc.

    So the above array is set up with:

    Array.SetValue("Names",1,"John")
    Array.SetValue("Names",2,"Fred")
    Array.SetValue("Names",3,"Mary")

    We can get the value corresponding to a given index using:

    name = Array.GetValue("Names",2)

    where name is then set to "Fred"

    Best to have a go or look at some of the samples people have uploaded, then post with any specific questions that arise.

    By the way when you mark a thread as answered, mark the answer rather than your statement that it is answered, since the answer is placed at the top following the question and then others can see the answer rather than your comment that it has been answered.

    • Marked as answer by SFC123 Tuesday, June 2, 2009 9:59 PM
    Tuesday, June 2, 2009 9:09 PM
    Moderator

All replies

  • You probably need to set up an array that contains previously read ItemNo.  Then use 'Array.ContainsValue' to check if the newly read value is in the array.  If it isn't then add it to the array.

    While ("true")
      ItemNo = TextWindow.Read()
      If (Array.ContainsValue("Items",ItemNo)) Then
        'Do something if we already have the ItemNo
      Else
        'Do something if ItemNo is new
        Array.SetValue("Items",Array.GetItemCount("Items")+1,ItemNo)
      EndIf
      'TextWindow.WriteLine(Array.GetItemCount("Items"))
    EndWhile


    Tuesday, June 2, 2009 8:26 PM
    Moderator
  • Thanks for the info!,
    I'm trying to set up the array that contains the previous ItemNo.
    Being new, i find Arrays tricky (even confusing) can you help me with this part too?
    Tuesday, June 2, 2009 8:41 PM
  • An array holds a set of related variables in a list such as a list of names.

    A array has a name which is a string - a label in quotes such as "Names"

    Each entry in the array has an index, usually a number running from 1, 2, 3 etc.

    So the array is a list with name and indexes to the items in the array (values).

    So an array called "Names" could include "John", "Fred" and "Marry", with indexes 1, 2 and 3.

    Index  Value
    1        "John"
    2        "Fred"
    3        "Mary"

    The indexes do not have to be sequential numbers, but they often are.
    The values can be of any type , numbers, strings, images, graphic objects etc - so we could hold for example an array of images in game.

    Look at the intellisense for Array to see the options to add, remove, check if the array contains a variable or index, get the number of variables in the array etc.

    So the above array is set up with:

    Array.SetValue("Names",1,"John")
    Array.SetValue("Names",2,"Fred")
    Array.SetValue("Names",3,"Mary")

    We can get the value corresponding to a given index using:

    name = Array.GetValue("Names",2)

    where name is then set to "Fred"

    Best to have a go or look at some of the samples people have uploaded, then post with any specific questions that arise.

    By the way when you mark a thread as answered, mark the answer rather than your statement that it is answered, since the answer is placed at the top following the question and then others can see the answer rather than your comment that it has been answered.

    • Marked as answer by SFC123 Tuesday, June 2, 2009 9:59 PM
    Tuesday, June 2, 2009 9:09 PM
    Moderator
  • Thanks that explains it nicely, it gives me a better understanding and how to use it.
    I will give it a try.
    Tuesday, June 2, 2009 10:02 PM