Table, row and column

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • embine
    Forum Member
    • Oct 2004
    • 11

    Table, row and column

    I have a table (shopping cart) with the following data in:

    2;;Rotherham - Case Study;;1
    1;;Wincanton - Case Study;;1
    1;;Wincanton - Case Study;;1
    2;;Rotherham - Case Study;;1


    The user can add and remove any number of items from their list.

    OK I can create a table from the text file:
    basket = TextFile.ReadToTable("C:\\MyFile.txt");
    I can return the indiviual rows:
    Dialog.Message("Test", basket[1], MB_OK, MB_ICONNONE, MB_DEFBUTTON1)
    Or
    Dialog.Message("Test", basket[2], MB_OK, MB_ICONNONE, MB_DEFBUTTON1)

    But how can I return the indiviual values from a row?
    i.e.
    Dialog.Message("Test", "Wincanton - Case Study", MB_OK, MB_ICONNONE, MB_DEFBUTTON1)
    Or
    Dialog.Message("Test", "1", MB_OK, MB_ICONNONE, MB_DEFBUTTON1)


    Any takers?
  • Worm
    Indigo Rose Customer
    • Jul 2002
    • 3967

    #2
    try:

    Dialog.Message("Test", basket[1][2], MB_OK, MB_ICONNONE, MB_DEFBUTTON1)

    [1] = row
    [2] = column

    Comment

    • JimS
      Indigo Rose Customer
      • May 2003
      • 1054

      #3
      How about after getting a line from your text file, you use the String.Replace action to replace the ;; with a comma, then read that in as a table that you can quarry. For this example I’ll refer to that ‘sub table’ as ‘sub_basket’

      Dialog.Message("Test", "Wincanton - Case Study", MB_OK, MB_ICONNONE, MB_DEFBUTTON1)

      might become something like:
      Dialog.Message("Test", sub-basket[2], MB_OK, MB_ICONNONE, MB_DEFBUTTON1)

      Comment

      • sferguson
        Indigo Rose Customer
        • Oct 2003
        • 164

        #4
        If you set the table up as an associative array then you could do something along these lines...

        Code:
        tBasket = { FirstElement="2", SecondElement="Rotherham - Case Study", ThirdElement="1"};
        
        Dialog.Message("Element 'first' contains:", tBasket.SecondElement);
        -Scott F.

        Comment

        • JimS
          Indigo Rose Customer
          • May 2003
          • 1054

          #5
          Sorry Worm, didn’t see your post. That’s cool, as soon as I get my head around it. Thanks, I learn something new each day.

          Comment

          • sferguson
            Indigo Rose Customer
            • Oct 2003
            • 164

            #6
            Right On! I was a bit slow on the reply button myself.
            -Scott F.

            Comment

            • embine
              Forum Member
              • Oct 2004
              • 11

              #7
              Doesn't work for me

              Hi Worm,

              Thanks for getting back to me.
              It doesn't work for me.

              -- Start load basket
              basket = TextFile.ReadToTable("C:\\MyFile.txt");
              basket_total = Table.Count(basket);
              Label.SetText("basket_ammount", basket_total);
              -- End load basket

              Dialog.Message("Test", basket[1][2], MB_OK, MB_ICONNONE, MB_DEFBUTTON1)

              if (basket ~= "") then
              for n=1, basket_total do
              ListBox.AddItem("basket_list", basket[n][2], "");
              end
              end


              I'd upload the project but the upload function doesn't work!!

              Comment

              • Worm
                Indigo Rose Customer
                • Jul 2002
                • 3967

                #8
                Oops! I apologize. I thought the data you were showing was merely a representation of the table, and the ;; was a field delimter. Now I see that your table actually has only 1 column which contains delimited fields. That being the case, you'll need to get a little more tricky.

                Here's a function that will return a field/column of a delimited string. Put it in your Global Functions.

                Code:
                function GetDelimField(sSourceIn, sDelim, nField)
                	nFieldCtr = 1
                	if String.Right(sSourceIn, String.Length(sDelim)) ~= sDelim then
                		sSourceIn = sSourceIn..sDelim
                	end
                	nFind = String.Find(sSourceIn, sDelim, 1, false)
                	sReturn = ""
                	if nFind ~= -1 then
                		while nFind > -1 do
                			if nFieldCtr == nField then
                				sReturn = String.Left(sSourceIn, nFind -1)
                				nFind = -1
                			else
                				sSourceIn = String.Mid(sSourceIn, nFind + String.Length(sDelim), -1)
                				nFind = String.Find(sSourceIn, sDelim, 1, false)
                				nFieldCtr = nFieldCtr + 1
                			end 
                		end
                	else
                		sReturn = sSourceIn
                	end	
                	return sReturn
                end
                Use it like this:

                Code:
                -- Start load basket
                basket = TextFile.ReadToTable("C:\\MyFile.txt");
                basket_total = Table.Count(basket);
                Label.SetText("basket_ammount", basket_total);
                -- End load basket
                
                Dialog.Message("Test", basket[1][2], MB_OK, MB_ICONNONE, MB_DEFBUTTON1)
                
                if (basket ~= "") then
                     for n=1, basket_total do
                          sResult = GetDelimField(basket[n],";;",2);
                          ListBox.AddItem("basket_list", sResult, "");
                     end
                end

                Comment

                • embine
                  Forum Member
                  • Oct 2004
                  • 11

                  #9
                  Fantastic Worm.

                  Cheers mate :yes

                  Comment

                  • embine
                    Forum Member
                    • Oct 2004
                    • 11

                    #10
                    Is there a better way to write out the text file?
                    I don't have to write it out in that format!

                    Comment

                    • Worm
                      Indigo Rose Customer
                      • Jul 2002
                      • 3967

                      #11
                      If you are getting the desired results, then no, there isn't a better way. There may be a more efficient way, but is recoding worth the miliseconds you might save, probably not.

                      What app are you writing the file out from?

                      Comment

                      • TJ_Tigger
                        Indigo Rose Customer
                        • Sep 2002
                        • 3159

                        #12
                        Originally posted by embine
                        Is there a better way to write out the text file?
                        I don't have to write it out in that format!
                        If you are are building a shopping cart, have you thought of using a SQL Database to store the information or even just a table until the shopper checks out? Does it need to be written to a file?
                        TJ-Tigger
                        "A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools."
                        "Draco dormiens nunquam titillandus."
                        Map of IR Forum Users - IR Project CodeViewer - Online Help - TiggTV - QuizEngine

                        Comment

                        • Worm
                          Indigo Rose Customer
                          • Jul 2002
                          • 3967

                          #13
                          Exactly where I was going Tig, that's why I asked what program was writing out the text file.

                          Comment

                          • embine
                            Forum Member
                            • Oct 2004
                            • 11

                            #14
                            No it doesn't need to be a text file, I guess it's the web developer in me wanting to write a cookie!!!
                            I guess I could create a SQlite database on the users PC just as easily!!

                            Thanks for the reality check guys.

                            PS
                            Have either of you decided about my proposal?

                            Comment

                            • embine
                              Forum Member
                              • Oct 2004
                              • 11

                              #15
                              The text file is being written out by AM5 by the way

                              Comment

                              Working...
                              X