locked
beginner Array Help RRS feed

  • Question

  • I have much trouble understanding how to program arrays. I'm want to read a text file that is a list of names, then the list is compared to a variable, if there is a match then a dialog box saying so. Is an array best for this? like  array.containsvalue could someone help me with the code?

    Sorry for the beginner programing but I just don't understand the way to program arrays. this seems to work but i had to create each name as a seperate text file,  is there a better way?

    path=program.directory 

      ItemDup = File.GetFiles(path + "\Items\")'directory of text files
      for ItemNoDupSearch = 1 To Array.GetItemCount(ItemDup)

        DupItem = File.ReadLine  (path + "\" Items List.txt",ItemNoDupSearch )'reads each line of text based on number of text files in "items" directory
        If ItemNo = DupItem then
          GraphicsWindow.ShowMessage ("That item number is already used, please choose another ","Item Number Already in Use")

        endif
      endfor

    Thanks,

    Steve

    Tuesday, January 25, 2011 11:27 PM

Answers

  • If you create an array you can save and read it to a single file with one command - the format of the file is not totally transparent, but if you are only using the file with SmallBasic this is the easiest way.:

    names[1] = "Mary"
    names[2] = "Fred"
    names[3] = "Andrew"

    File.WriteContents(Program.Directory+"\names.txt",names)
    names = "" 'clear the names array to make sure it works
    names = File.ReadContents(Program.Directory+"\names.txt")

    'Test it worked
    For i = 1 To Array.GetItemCount(names)
      TextWindow.WriteLine(names[i])
    EndFor

    Once you have read the array, you can test it element by element in a loop or use the Array.ContainsValue command - the latter is simpler but requires an exact match , for example it is case sensitive - using a loop you can for example convert to lower case to do a test or check if the test name is a sub text of an array element.

    The approach is to separate the read/write of the file from the test of whether a match is found.

    • Marked as answer by SFC-P33A Thursday, January 27, 2011 5:30 PM
    Wednesday, January 26, 2011 9:36 PM

All replies

  • Arrays are a single variable consisting of a string: 1=hello;2=my;3=name;4=is;5=Airwaves;. These are best used in loops, especially in For loops, because you can use the value of the For loop to retrieve data from that variable.

    They can be used to save and retrieve data from a variable:

    arraylist[1]="Hello"

    arraylist[2]="My"

    arraylist[3]="Name"

    arraylist[4]="Is"

    arraylist[5]="Airwaves"

    For i = 1 To Array.GetItemCount(arraylist)

      TextWindow.WriteLine(arraylist[i] + " ")

    EndFor

     

    Arrays are useful for programs using multiple values of the same type, like in a shooting game, you can use:

     

    For i = 1 To 5

      bullet[i] = Shapes.AddRectangle(10, 10)

    EndFor


    ~~AirWaves!!~~
    Wednesday, January 26, 2011 2:44 AM
  • If you create an array you can save and read it to a single file with one command - the format of the file is not totally transparent, but if you are only using the file with SmallBasic this is the easiest way.:

    names[1] = "Mary"
    names[2] = "Fred"
    names[3] = "Andrew"

    File.WriteContents(Program.Directory+"\names.txt",names)
    names = "" 'clear the names array to make sure it works
    names = File.ReadContents(Program.Directory+"\names.txt")

    'Test it worked
    For i = 1 To Array.GetItemCount(names)
      TextWindow.WriteLine(names[i])
    EndFor

    Once you have read the array, you can test it element by element in a loop or use the Array.ContainsValue command - the latter is simpler but requires an exact match , for example it is case sensitive - using a loop you can for example convert to lower case to do a test or check if the test name is a sub text of an array element.

    The approach is to separate the read/write of the file from the test of whether a match is found.

    • Marked as answer by SFC-P33A Thursday, January 27, 2011 5:30 PM
    Wednesday, January 26, 2011 9:36 PM
  • Thank you for your help AirWaves
    Thursday, January 27, 2011 5:23 PM
  • Thany you litdev, that is helpful
    Thursday, January 27, 2011 5:30 PM