PDA

View Full Version : Table, row and column


embine
12-15-2004, 06:31 AM
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
12-15-2004, 06:56 AM
try:

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

[1] = row
[2] = column

JimS
12-15-2004, 06:59 AM
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)

sferguson
12-15-2004, 06:59 AM
If you set the table up as an associative array then you could do something along these lines...

tBasket = { FirstElement="2", SecondElement="Rotherham - Case Study", ThirdElement="1"};

Dialog.Message("Element 'first' contains:", tBasket.SecondElement);

JimS
12-15-2004, 07:01 AM
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.

sferguson
12-15-2004, 07:02 AM
Right On! I was a bit slow on the reply button myself.

embine
12-15-2004, 08:02 AM
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!!

Worm
12-15-2004, 08:15 AM
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.


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:


-- 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

embine
12-15-2004, 11:00 AM
Fantastic Worm.

Cheers mate :yes

embine
12-15-2004, 11:01 AM
Is there a better way to write out the text file?
I don't have to write it out in that format!

Worm
12-15-2004, 11:31 AM
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?

TJ_Tigger
12-15-2004, 07:56 PM
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?

Worm
12-15-2004, 08:13 PM
Exactly where I was going Tig, that's why I asked what program was writing out the text file.

embine
12-16-2004, 09:26 AM
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? ;)

embine
12-16-2004, 09:28 AM
The text file is being written out by AM5 by the way

Worm
12-16-2004, 09:53 AM
Instead of writing out a text file, write to the table directly. It'd save a step.