none
help with small basic arrays RRS feed

  • Question

  • Hi, i'm trying to write a small basic program using arrays. I must have read the manual over 10 times and looked at examples but just can't seem to sort it out. I'd like to enter customer info / product / equipment and recall any record by searching the arrays. Please help me thanks.

    ' For x = 1 To 5
     ' TextWindow.Write("Enter customer's name: ")
     ' cust[index]["name"] = TextWindow.Read()
     ' TextWindow.Write("Enter product: ")
     ' cust[index]["product"] = TextWindow.Read()
     ' TextWindow.Write("Enter equipment: ")
     ' cust[index]["equipment"] = TextWindow.Read()
     ' EndFor
     ' 
     ' ' Recalls any record
     ' TextWindow.Write("Search customer information: ")
     ' index = TextWindow.Read()
    '
    '     TextWindow.WriteLine("Customer: " +cust[index])
    '     TextWindow.WriteLine("Product: " +cust[index])
    '     TextWindow.WriteLine("Equipment: " +cust[index])

    Wednesday, October 9, 2013 12:34 AM

Answers

  • Hi playerb!

    Here is your code fixed:

    For x = 1 To 5
       TextWindow.Write("Enter customer's name: ")
       cust[x]["name"] = TextWindow.Read()
       TextWindow.Write("Enter product: ")
       cust[x]["product"] = TextWindow.Read()
       TextWindow.Write("Enter equipment: ")
       cust[x]["equipment"] = TextWindow.Read()
    EndFor
    
    ' Recalls any record
    TextWindow.Write("Search customer information: ")
    index = TextWindow.Read()
    
    TextWindow.WriteLine("Customer: " +cust[index]["name"])
    TextWindow.WriteLine("Product: " +cust[index]["product"])
    TextWindow.WriteLine("Equipment: " +cust[index]["equipment"])

    You just made two small mistakes:

    1) You should have used "x" rather than "index" as the first array element when making the assignments in the for loop. "index" will keep its default value of zero.

    2) You just forgot to use the ["name"], ["product"] and ["equipment"] elements to get at the values you are printing.


    Please mark any answers and "vote as helpful" any posts that help you!

    Wednesday, October 9, 2013 1:21 AM
    Answerer

All replies

  • Hi playerb!

    Here is your code fixed:

    For x = 1 To 5
       TextWindow.Write("Enter customer's name: ")
       cust[x]["name"] = TextWindow.Read()
       TextWindow.Write("Enter product: ")
       cust[x]["product"] = TextWindow.Read()
       TextWindow.Write("Enter equipment: ")
       cust[x]["equipment"] = TextWindow.Read()
    EndFor
    
    ' Recalls any record
    TextWindow.Write("Search customer information: ")
    index = TextWindow.Read()
    
    TextWindow.WriteLine("Customer: " +cust[index]["name"])
    TextWindow.WriteLine("Product: " +cust[index]["product"])
    TextWindow.WriteLine("Equipment: " +cust[index]["equipment"])

    You just made two small mistakes:

    1) You should have used "x" rather than "index" as the first array element when making the assignments in the for loop. "index" will keep its default value of zero.

    2) You just forgot to use the ["name"], ["product"] and ["equipment"] elements to get at the values you are printing.


    Please mark any answers and "vote as helpful" any posts that help you!

    Wednesday, October 9, 2013 1:21 AM
    Answerer
  • Hi, player

    Here's a way to do it that allows you to search by name:

    For x = 1 To 5
       TextWindow.Write("Enter customers name: ")
       cust[x]["name"] = TextWindow.Read()
       TextWindow.Write("Enter product: ")
       cust[x]["product"] = TextWindow.Read()
       TextWindow.Write("Enter equipment: ")
       cust[x]["equipment"] = TextWindow.Read()
    endfor
       
       ' Recalls any record
       TextWindow.Write("Search customer information: ")
       dataInput = TextWindow.Read()
       
       For index = 1 To 5
         
         If cust[index]["name"] = dataInput then
          TextWindow.WriteLine("Customer: " +cust[index]["name"])
          TextWindow.WriteLine("Product: " +cust[index]["product"])
          TextWindow.WriteLine("Equipment: " +cust[index]["equipment"])
        EndIf
      EndFor
    

    Wednesday, October 9, 2013 1:30 AM
  • Thank you SO much! Now that I look at my code, I was close but I got just frustrated and could not find the mistake. If I made a mistake entering the info and want to modify or replace the info i.e name,product or equipment how would I go about re-calling the info to be modified? I would probable have to retrieve the array somehow?

    thanking you in advance

    Thursday, October 10, 2013 12:37 AM
  • Thanks Anthony, that's exactly what I was aiming for. Just thinking ahead and how to modify the information in the arrays. How would I start this off?

    thank you again guys

    Thursday, October 10, 2013 12:40 AM
  • Glad we could help!

    OK, imagine you wanted to change customer 1's name. You could do this:

    cust[1]["name"] = "Bob"

    (replacing "Bob" with whatever you choose, or a variable containing the name, etc.)

    I always liked to visualize 2-dimensional arrays as tables like this:

             NAME     PRODUCT     EQUIPMENT
    INDEX
    
      1      "Bob"    "Sample"    "Toaster"
      2      "Jim"   "Sample2"     "Oven"
      3      ....      ......       .....
      4      ....      ......       .....
      5      ....      ......       .....

    So, to change customer 2's equipment we could do:

    cust[2]["equipment"] = "NewValue"
    Hope this helps!


    Please mark any answers and "vote as helpful" any posts that help you!

    Thursday, October 10, 2013 12:44 AM
    Answerer
  • This is graphic version . PLZ tryand  recognize where data is stored(underlined).  

    GUI()
    Controls.ButtonClicked=OnClicked

    Sub OnClicked
      nm= Controls.GetButtonCaption(Controls.LastClickedButton)
      If nm= "Enter" Then
        NUMBER=Controls.GetTextBoxText(TBOX[1])   '  get Number
        Rawdata=""
        For i=2 To 4
          cust[Number][MSG[i]]=Controls.GetTextBoxText(TBOX[i])  ' data is stored here
          Rawdata=Rawdata+MSG[i]+"= "+cust[Number][MSG[i]]+CRLF  '  connect data
          Controls.SetTextBoxText(TBOX[i],"")   ' erase  each data
        EndFor
        Rawdata="Number= "+Number+CRLF+Rawdata
        LST=LST+Rawdata+CRLF                    '  List up data
        Shapes.SetText(Showdata,Rawdata)        '  show  last data
        Controls.SetTextBoxText(TBOX[1],"")
      ElseIf nm= "Recall" then 
        Controls.SetTextBoxText(MBOX,LST)       '  recall  all data
      EndIf
    EndSub

    Sub GUI
      GraphicsWindow.Width=930
      GraphicsWindow.Height=600
      GraphicsWindow.BackgroundColor="Darkgreen"
      GraphicsWindow.BrushColor="Red"
      GraphicsWindow.FontSize=20
      MSG="1=Number;2=Customer;3=Product;4=Equipment"
      For i=1 to 4
        GraphicsWindow.BrushColor="Yellow"
        GraphicsWindow.DrawText(15,5+60*i,MSG[i])
        GraphicsWindow.BrushColor="Navy"
        TBOX[i]= Controls.AddTextBox(150,60*i)    '   make text boxes
        Controls.SetSize(TBOX[i],300,45)
      EndFor
      Controls.SetSize(TBOX[1],50,45)
      ENTER= Controls.AddButton("Enter",250,60)   '  Enter button
      MBOX= Controls.AddMultiLineTextBox(500,60)  '   MultiLineTextBox  for recalled data 
      Controls.SetSize(MBOX,400,500)
      RECALL=Controls.AddButton("Recall",550,10)  '  Recall button 
      CRLF= Text.GetCharacter(13)+Text.GetCharacter(10) '  carriage  return  &  line feed
      Showdata= Shapes.AddText(" ")               '  shape text
      Shapes.Move(Showdata,150,350)
    EndSub




    Thursday, October 10, 2013 4:18 AM
    Answerer
  • I've modified the code to search for name or product or equipment but it prints it vertically downwards.I'd really like to know how to represent the data as a table instead of the way I wrote it initially i.e with the header(if possible). I'm still uncertain but would it be something to this effect. (sorry its confusing.)

    Fori = 1 ToArray.GetItemCount(cust)

    TextWindow.WriteLine(custi[dataInput]["name: "]+custi[dataInput]["equipment: "]+ custi[dataInput]["product #: "]+custi[dataInput])

    EndFor

    One more thing, I've changed the product to product # and noticed that dataInput = TextWindow.Read() accepts a numerical value, is that normal? Thought I should use TextWindow.ReadNumber() instead but that won't work for text input.

    thanks again

    Thursday, October 10, 2013 6:34 AM
  • OMG!! this is nice but for me (newbie) its confusing as I'm just beginning. I really need to understand the code before I do anything like that. But its very nice, thank you very much for spending time on it.
    Thursday, October 10, 2013 6:37 AM
  • Hi, Playerb,

    I believe it is normal for Textwindow.Read() to accept either letters or numbers. If you only wanted to get numbers you would use TextWindow.ReadNumber() instead.

    Regarding displaying array data as a table, here's an example:

    cust[1]["name"] = "Luigi"
    cust[1]["equipment"]="Laptop"
    cust[1]["product #"]="Pavilion"
    
    cust[2]["name"] = "Mario"
    cust[2]["equipment"]="Desktop"
    cust[2]["product #"]="Gateway"
    
    cust[3]["name"] = "Zelda"
    cust[3]["equipment"]="IPad"
    cust[3]["product #"]="Apple"
    
    For i = 1 to Array.GetItemCount(cust)
    TextWindow.WriteLine(cust[i]["name"]+"   "+cust[i]["equipment"]+"   "+cust[i]["product #"])
    EndFor

    Friday, October 11, 2013 11:39 PM
  • Hi all,

    Good thread :) - I have picked up a few good tips for using arrays.

    PlayerB - I thought you might be asking for a way to search your array using name or equipment or product# ...

    If so - here is a way of doing it I put together: (NB: I have amended the starting array)

    cust[1]["name"] = "Luigi"
    cust[1]["equipment"]="Laptop"
    cust[1]["product #"]="Apple"
    
    cust[2]["name"] = "Mario"
    cust[2]["equipment"]="Laptop"
    cust[2]["product #"]="Gateway"
    
    cust[3]["name"] = "Zelda"
    cust[3]["equipment"]="IPad"
    cust[3]["product #"]="Apple"
    
    TextWindow.WriteLine("A - Name")
    TextWindow.WriteLine("B - Equipment")
    TextWindow.Writeline("C - Product #")
    TextWindow.Writeline(" ")
    TextWindow.Write("Please enter letter from list above to search on:")
    Search_field = TextWindow.Read()
    TextWindow.Write("Please enter search term:")
    Search_term = TextWindow.Read()
    
    If Search_field = "A" Then
      For i = 1 to Array.GetItemCount(cust)
        If cust[i]["name"] = Search_term then
          TextWindow.WriteLine(cust[i]["name"]+":"+"   "+cust[i]["equipment"]+"   "+cust[i]["product #"])
        EndIf
      EndFor
        
    ElseIf Search_field = "B" then
      For i = 1 to Array.GetItemCount(cust)
        If cust[i]["equipment"] = Search_term then
          TextWindow.WriteLine(cust[i]["equipment"]+":"+"   "+cust[i]["name"]+"   "+cust[i]["product #"])
        EndIf
      EndFor
        
    Elseif Search_field = "C" then
      For i = 1 to Array.GetItemCount(cust)
        If cust[i]["product #"] = Search_term then
          TextWindow.WriteLine(cust[i]["product #"]+":"+"   "+cust[i]["name"]+"   "+cust[i]["equipment"])
        EndIf
      Endfor
    EndIf

    I am asking the user:

    1) to choose whether they want to search the name field, equipment field or product field

    For each choice here the if statement directs the computer to the right piece of code to carry out the search - so if the user inputs A the code that follows carries out the right search for that field

    2) What they want to search for in that field

    The for statements all do the same basic thing but they are written to search on the correct field and term as input by the user.

    The for statements check each customer record against the user input - if the user inputs B, Laptop the for statement checks each customer record to see if it has Laptop as equipment. Any record that does is then printed to screen with the equipment first followed by the other fields.

    Hope this is helpful + keep on coding!

    Daniel


    Monday, December 16, 2013 11:40 PM
  • Hi PlayerB

    I just re-read your post and I dont think that is what you were asking - sorry! Anyway ... I enjoyed working on it :)

    Daniel

    Monday, December 16, 2013 11:45 PM
  • Here is another way to use arrays:

    For i = 1 To 10
      Ball[i] = Shapes.AddEllipse(10,10)
      Ballx[i] = i * 12
      Bally[i] = 10
    EndFor
    
    While 1 = 1
      Program.Delay(5)
      For t = 1 To 10
        Bally[t] = Bally[t] + 1
        Shapes.Move(Ball[t], Ballx[t],Bally[t])
      EndFor
      EndWhile


    It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11

    Friday, December 20, 2013 4:08 PM
    Answerer