Page 1 of 1

How can I check values for a table column?

Posted: Sat Apr 28, 2012 5:30 am
by glenn52
:cry:
I have a simple table filled from a text file.
It comprises a number of columns.
I need to check & see if all values in a column are equal to n

How can I determine if item 2 of line 1 to tNumberOfLines of field "myTable" = n ?

I can Repeat through each line to check the values but was wondering if there was something simpler...

Thanks in advance for any assistance!

LiveCode 4.6.4

Re: How can I check values for a table column?

Posted: Sat Apr 28, 2012 11:13 am
by Klaus
Hi Glen,

"repeat for each line tLine..." will surely be the fastest way to do wat you want!

Another way may to use an ARRAY and extract the wanted column from that:

Code: Select all

...
put field "your field here" into tArray

## Turn it into an array:
split tArray by column

## Extract your second column
put tArray[2] into tColumn

## I will use the SUM function which demands a comma delimited itemlist
replace CR with "," in tColumn

## Now you got what you need:
put sum(tColumn) into MyN
...
I doubt this is faster than the first method, but there are always may ways to skin a cat in LiveCode :D

I would put this into a function like this:

## With tColumn you could also add the number of your desired column,
## so this finction is even more "versatile"
## Of course you can "hard code" the column number and omit that parameter.

Code: Select all

function addColumns tData, tColumn
  split tData by column

  ## Extract your seocnd column
  put tData[tColumn] into tColumn1

  ## I will use the SUM function which demands a comma delimited itemlist
  replace CR with "," in tColumn1

  ## Now you got what you need:
  return sum(tColumn1) 
end addColumns
Then call it with:
...
put fld "xyz" into tData
answer addColumns(tData,2)
...


Best

Klaus

Re: How can I check values for a table column?

Posted: Sat Apr 28, 2012 12:13 pm
by glenn52
Thanks Claus!

The repeat is what I had implemented but wondered about an alternative.
I'll stick with repeat at this time.

Thanks again :wink:

Re: How can I check values for a table column?

Posted: Sat Apr 28, 2012 12:16 pm
by Klaus
Hi Glenn,
glenn52 wrote:Thanks Claus!
Klaus with a K, my first name is not Santa :D


Best

Klaus

Re: How can I check values for a table column?

Posted: Sat Apr 28, 2012 12:28 pm
by glenn52
Klaus wrote:Hi Glen
Don't you just hate it when someone can't get your name right... :wink:

Re: How can I check values for a table column?

Posted: Sat Apr 28, 2012 1:30 pm
by Klaus
Hi Glenn (sic!),

you are damn right, my friend! :D

How embarrassing... :oops:


Best

Klaus

Re: How can I check values for a table column?

Posted: Sat Apr 28, 2012 2:13 pm
by sturgis
Working directly with the text of a field won't be as fast as dumping it in to a variable or array but..
The itemdelimiter of a table field is tab. So to get the value of item 2 line 5 and see if its = n you can do this

Code: Select all

set the itemdelimiter to tab
if (item 2 line 5 of field "fieldname") = 12 then put "YAY!"
If you want to use this method, put the contents into a variable first and work with that (same though, set the itemdelimiter to tab)

Having said all this? I prefer the Klaus Array Method(tm)
glenn52 wrote::cry:
I have a simple table filled from a text file.
It comprises a number of columns.
I need to check & see if all values in a column are equal to n

How can I determine if item 2 of line 1 to tNumberOfLines of field "myTable" = n ?

I can Repeat through each line to check the values but was wondering if there was something simpler...

Thanks in advance for any assistance!

LiveCode 4.6.4