locked
SB Multi dimensional Array text search ..Help RRS feed

  • Question

  • Hi, I'm trying to run a search in my multi dimensional array for a match , can someone help me out. 

    This is part of the code, but I don't know how to structure it

             

      TextWindow.Write("Search Car Records, Enter Car Name, yr or color: ")
        searchrec = TextWindow.Read()

       TextWindow.Write("Checking if records exist, please wait...")


     For i = 1 To 5   'There are 5 entries, to keep it simple
         If Text.IsSubText(arrCarName[i],searchrec) Then
         TextWindow.WriteLine("Yes, Car exists in records")

       Else
        TextWindow.WriteLine("No, Car was not found...please re-enter: ")       
       EndIf
       EndFor

    This is the other criteria to search for in the array
         if Text.IsSubText(arrCarInfo[arrCarName[i]]["year"],searchrec) then
         if Text.IsSubText(arrCarInfo[arrCarName[i]]["color"],searchrec) then

    is there a simpler, cleaner way of doing this?

     thanks 
    Tuesday, November 5, 2013 3:45 AM

Answers

  • To check whether an Array contains some value, use method:

    Array.ContainsValue().

    And seems like you're also creating a 2D Array called arrCarInfo.

    And you intend to use a string instead of a numerical value as its key index.

    It's a little harder to deal with, but doable!  :D

    Take a look at my "Login System Example" to see how I did it once:   :P

    i-an-amateur-messed-up-again-the-stuff-i-worked-on-today-isnt-cooperating-help


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    • Edited by GoToLoopEditor Tuesday, November 5, 2013 5:25 AM
    • Proposed as answer by litdev Friday, November 15, 2013 6:49 PM
    • Marked as answer by gungan37Editor Saturday, November 16, 2013 4:33 AM
    Tuesday, November 5, 2013 5:23 AM
    Answerer
  • If someone has any ideas or examples please let me know

    I appreciate the help

    thanks

    How about this?     

    ' for example
    arrCarInfo[1]="Carname=Toyota;Color=white;Year=1985;Type=Sedan"   '  Open  Hatchback  Wagon  etc
    arrCarInfo[2]="Carname=VolksWagen;Color=Black;Year=2001;Type=Coupe"
    arrCarInfo[3]="Carname=Nissan;Color=Black;Year=2008;Type=Sports"

    While "true"
      TextWindow.Write("Search Car Records, Enter Car Name, yr , color or Type: ")
      searchrec = TextWindow.Read()
      TextWindow.Writeline("Checking if records exist, please wait...")
      TF="False"
      For i=1 To Array.GetItemCount(arrCarInfo)
        If Text.IsSubText(arrCarInfo[i],searchrec) Then
          TextWindow.WriteLine("Yes, Car exists in records")
          TextWindow.WriteLine(i+" : "+arrCarInfo[i])
          TF="True"
        EndIf
      EndFor
      If TF="False" then
        TextWindow.WriteLine("No, Car was not found...please re-enter: ")      
      EndIf
      TextWindow.WriteLine(" ") 
    endwhile

    * Note     arrCarInfo[2]="Carname=VolksWagen;Color=Black;Year=2001;Type=Coupe"
          is same as follows.
    arrCarInfo[2]["Carname"]="VolksWagen"
    arrCarInfo[2]["Color"]="Black"
    arrCarInfo[2]["Year"]="2001"
    arrCarInfo[2]["Type"]="Coupe"

     



    • Edited by NaochanONEditor Wednesday, November 6, 2013 5:30 AM add comment
    • Proposed as answer by litdev Friday, November 15, 2013 6:49 PM
    • Marked as answer by gungan37Editor Saturday, November 16, 2013 4:33 AM
    Wednesday, November 6, 2013 1:31 AM
    Answerer
  • Hi, this while/endwhile loop goes on forever. Can you tell me how to break out of the loop to move on to a following editCar subroutine? and could you please explain how the flags are determining if the car was found or not? Thank you very much for your help.

    1)Searching method.....  Text.IsSubtext() seraches a word in a time

    If you input  serachrec="2008" ,
    For Loop increases I Number.
    When I=3 ,
    arrCarInfo[3] ="Carname=Nissan;Color=Black;Year=2008;Type=Sports"" and
    it includes "2008"
    So, Text.IsSubText(arrCarInfo[3],serachrec) returns "True"
    (It means Text.IsSubtext() seraches a word in a time.)
    If serachrec="Coupe" ,
    For Loop increases I Number.
    When I=2 ,
    arrCarInfo[2] = "Carname=VolksWagen;Color=Black;Year=2001;Type=Coupe"
    it  includes "Coupe"
    So, Text.IsSubText(arrCarInfo[3],serachrec) returns "True"
    If you input serachrec="Green" (or other word not contained in arrCarInfo array)
    arrCarInfo[1], arrCarInfo[2], arrCarInfo[3] doesn't include "Green" ,
    So, Text.IsSubText(arrCarInfo[I],serachrec) returns "False"  
    and exit while loop

    2)How to exit while loop.....    Sorry underlined parts are necessary.

    arrCarInfo[1]="Carname=Toyota;Color=white;Year=1985;Type=Sedan"   '  Open  Hatchback  Wagon  etc
    arrCarInfo[2]="Carname=VolksWagen;Color=Black;Year=2001;Type=Coupe"
    arrCarInfo[3]="Carname=Nissan;Color=Black;Year=2008;Type=Sports"
    TF="True"
    While TF="True"
      TextWindow.Write("Search Car Records, Enter Car Name, yr , color or Type: ")
      searchrec = TextWindow.Read()
      TextWindow.Writeline("Checking if records exist, please wait...")
      TF="False"
      For i=1 To Array.GetItemCount(arrCarInfo)
        If Text.IsSubText(arrCarInfo[i],searchrec) Then
          TextWindow.WriteLine("Yes, Car exists in records")
          TextWindow.WriteLine(i+" : "+arrCarInfo[i])
          TF="True"
        EndIf
      EndFor
      If TF="False" then
        TextWindow.WriteLine("No, Car was not found...please re-enter: ")      
      EndIf
      TextWindow.WriteLine(" ") 
    endwhile
     

    * if you want to know car exist or not,  below code is simple.

    arrCarInfo[1]="Carname=Toyota;Color=white;Year=1985;Type=Sedan"   '  Open  Hatchback  Wagon  etc
    arrCarInfo[2]="Carname=VolksWagen;Color=Black;Year=2001;Type=Coupe"
    arrCarInfo[3]="Carname=Nissan;Color=Black;Year=2008;Type=Sports"
    Ans="True=Yes, Car was found;False=Sorry, Car was not found"
    TF="True"
    While  TF="True"
      TextWindow.Write("Search Car Records, Enter Car Name, yr , color or Type: ")
      TextWindow.Writeline("Checking if records exist, please wait...")
      TrueFalse=Text.IsSubText(arrCarInfo,TextWindow.Read())
      TextWindow.WriteLine(Ans[TrueFalse])
      TextWindow.WriteLine(" ") 
      TF=TrueFalse
    endwhile



    • Edited by NaochanONEditor Saturday, November 9, 2013 4:32 AM add code
    • Proposed as answer by litdev Friday, November 15, 2013 6:49 PM
    • Marked as answer by gungan37Editor Saturday, November 16, 2013 4:33 AM
    Friday, November 8, 2013 8:20 AM
    Answerer

All replies

  • To check whether an Array contains some value, use method:

    Array.ContainsValue().

    And seems like you're also creating a 2D Array called arrCarInfo.

    And you intend to use a string instead of a numerical value as its key index.

    It's a little harder to deal with, but doable!  :D

    Take a look at my "Login System Example" to see how I did it once:   :P

    i-an-amateur-messed-up-again-the-stuff-i-worked-on-today-isnt-cooperating-help


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    • Edited by GoToLoopEditor Tuesday, November 5, 2013 5:25 AM
    • Proposed as answer by litdev Friday, November 15, 2013 6:49 PM
    • Marked as answer by gungan37Editor Saturday, November 16, 2013 4:33 AM
    Tuesday, November 5, 2013 5:23 AM
    Answerer
  • I looked at your code and I can't figure out where exactly you check and confirm the user pw in the array.

    I'm a newbie so I need something simple.

    If someone has any ideas or examples please let me know

    I appreciate the help

    thanks

    Tuesday, November 5, 2013 11:26 PM
  • If someone has any ideas or examples please let me know

    I appreciate the help

    thanks

    How about this?     

    ' for example
    arrCarInfo[1]="Carname=Toyota;Color=white;Year=1985;Type=Sedan"   '  Open  Hatchback  Wagon  etc
    arrCarInfo[2]="Carname=VolksWagen;Color=Black;Year=2001;Type=Coupe"
    arrCarInfo[3]="Carname=Nissan;Color=Black;Year=2008;Type=Sports"

    While "true"
      TextWindow.Write("Search Car Records, Enter Car Name, yr , color or Type: ")
      searchrec = TextWindow.Read()
      TextWindow.Writeline("Checking if records exist, please wait...")
      TF="False"
      For i=1 To Array.GetItemCount(arrCarInfo)
        If Text.IsSubText(arrCarInfo[i],searchrec) Then
          TextWindow.WriteLine("Yes, Car exists in records")
          TextWindow.WriteLine(i+" : "+arrCarInfo[i])
          TF="True"
        EndIf
      EndFor
      If TF="False" then
        TextWindow.WriteLine("No, Car was not found...please re-enter: ")      
      EndIf
      TextWindow.WriteLine(" ") 
    endwhile

    * Note     arrCarInfo[2]="Carname=VolksWagen;Color=Black;Year=2001;Type=Coupe"
          is same as follows.
    arrCarInfo[2]["Carname"]="VolksWagen"
    arrCarInfo[2]["Color"]="Black"
    arrCarInfo[2]["Year"]="2001"
    arrCarInfo[2]["Type"]="Coupe"

     



    • Edited by NaochanONEditor Wednesday, November 6, 2013 5:30 AM add comment
    • Proposed as answer by litdev Friday, November 15, 2013 6:49 PM
    • Marked as answer by gungan37Editor Saturday, November 16, 2013 4:33 AM
    Wednesday, November 6, 2013 1:31 AM
    Answerer
  • playerb,

    If you use Array.GetAllIndices, Array.GetItemCount and Array.ContainsValue you can look up text indices pretty easy.

    When working out your search use writelines to see how the array is stored,

    e.g. TextWindow.WriteLine(Array.GetAllIndices(arrCarInfo))

    here's a sample for your task  XSP794

    Wednesday, November 6, 2013 6:21 AM
  • I tweaked my code a little and this works out perfectly. Although I don't like the way this line  TextWindow.WriteLine(i+" : "+arrCarInfo[i]) prints out the result. is there a cleaner way to show the details of the records?

    Thank you

    Wednesday, November 6, 2013 7:58 PM
  • Hi, how do I get to your sample?  XSP794

    thanks

    Wednesday, November 6, 2013 8:00 PM
  • From the Small Basic IDE (interface) click the Import button on the top bar.  Then copy/paste or type the ID in.
    Wednesday, November 6, 2013 8:06 PM
  • You can also visit this link to see it running online w/ Silverlight plugin:

    XSP794


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Wednesday, November 6, 2013 8:17 PM
    Answerer
  • Hi, this while/endwhile loop goes on forever. Can you tell me how to break out of the loop to move on to a following editCar subroutine? and could you please explain how the flags are determining if the car was found or not? Thank you very much for your help.

    While "true"
      TextWindow.Write("Search Car Records, Enter Car Name, yr , color or Type: ")
      searchrec = TextWindow.Read()
      TextWindow.Writeline("Checking if records exist, please wait...")
      TF="False"
      For i=1 To Array.GetItemCount(arrCarInfo)
        If Text.IsSubText(arrCarInfo[i],searchrec) Then
          TextWindow.WriteLine("Yes, Car exists in records")
          TextWindow.WriteLine(i+" : "+arrCarInfo[i])
          TF="True"
        EndIf
      EndFor
      If TF="False" then
        TextWindow.WriteLine("No, Car was not found...please re-enter: ")      
      EndIf
      TextWindow.WriteLine(" ") 
    endwhile

    Friday, November 8, 2013 7:09 AM
  • Hi, this while/endwhile loop goes on forever. Can you tell me how to break out of the loop to move on to a following editCar subroutine? and could you please explain how the flags are determining if the car was found or not? Thank you very much for your help.

    1)Searching method.....  Text.IsSubtext() seraches a word in a time

    If you input  serachrec="2008" ,
    For Loop increases I Number.
    When I=3 ,
    arrCarInfo[3] ="Carname=Nissan;Color=Black;Year=2008;Type=Sports"" and
    it includes "2008"
    So, Text.IsSubText(arrCarInfo[3],serachrec) returns "True"
    (It means Text.IsSubtext() seraches a word in a time.)
    If serachrec="Coupe" ,
    For Loop increases I Number.
    When I=2 ,
    arrCarInfo[2] = "Carname=VolksWagen;Color=Black;Year=2001;Type=Coupe"
    it  includes "Coupe"
    So, Text.IsSubText(arrCarInfo[3],serachrec) returns "True"
    If you input serachrec="Green" (or other word not contained in arrCarInfo array)
    arrCarInfo[1], arrCarInfo[2], arrCarInfo[3] doesn't include "Green" ,
    So, Text.IsSubText(arrCarInfo[I],serachrec) returns "False"  
    and exit while loop

    2)How to exit while loop.....    Sorry underlined parts are necessary.

    arrCarInfo[1]="Carname=Toyota;Color=white;Year=1985;Type=Sedan"   '  Open  Hatchback  Wagon  etc
    arrCarInfo[2]="Carname=VolksWagen;Color=Black;Year=2001;Type=Coupe"
    arrCarInfo[3]="Carname=Nissan;Color=Black;Year=2008;Type=Sports"
    TF="True"
    While TF="True"
      TextWindow.Write("Search Car Records, Enter Car Name, yr , color or Type: ")
      searchrec = TextWindow.Read()
      TextWindow.Writeline("Checking if records exist, please wait...")
      TF="False"
      For i=1 To Array.GetItemCount(arrCarInfo)
        If Text.IsSubText(arrCarInfo[i],searchrec) Then
          TextWindow.WriteLine("Yes, Car exists in records")
          TextWindow.WriteLine(i+" : "+arrCarInfo[i])
          TF="True"
        EndIf
      EndFor
      If TF="False" then
        TextWindow.WriteLine("No, Car was not found...please re-enter: ")      
      EndIf
      TextWindow.WriteLine(" ") 
    endwhile
     

    * if you want to know car exist or not,  below code is simple.

    arrCarInfo[1]="Carname=Toyota;Color=white;Year=1985;Type=Sedan"   '  Open  Hatchback  Wagon  etc
    arrCarInfo[2]="Carname=VolksWagen;Color=Black;Year=2001;Type=Coupe"
    arrCarInfo[3]="Carname=Nissan;Color=Black;Year=2008;Type=Sports"
    Ans="True=Yes, Car was found;False=Sorry, Car was not found"
    TF="True"
    While  TF="True"
      TextWindow.Write("Search Car Records, Enter Car Name, yr , color or Type: ")
      TextWindow.Writeline("Checking if records exist, please wait...")
      TrueFalse=Text.IsSubText(arrCarInfo,TextWindow.Read())
      TextWindow.WriteLine(Ans[TrueFalse])
      TextWindow.WriteLine(" ") 
      TF=TrueFalse
    endwhile



    • Edited by NaochanONEditor Saturday, November 9, 2013 4:32 AM add code
    • Proposed as answer by litdev Friday, November 15, 2013 6:49 PM
    • Marked as answer by gungan37Editor Saturday, November 16, 2013 4:33 AM
    Friday, November 8, 2013 8:20 AM
    Answerer