How can I check values for a table column?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
glenn52
Posts: 25
Joined: Mon May 02, 2011 1:34 pm
Contact:

How can I check values for a table column?

Post by glenn52 » Sat Apr 28, 2012 5:30 am

: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

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

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

Post by Klaus » Sat Apr 28, 2012 11:13 am

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

glenn52
Posts: 25
Joined: Mon May 02, 2011 1:34 pm
Contact:

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

Post by glenn52 » Sat Apr 28, 2012 12:13 pm

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:

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

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

Post by Klaus » Sat Apr 28, 2012 12:16 pm

Hi Glenn,
glenn52 wrote:Thanks Claus!
Klaus with a K, my first name is not Santa :D


Best

Klaus

glenn52
Posts: 25
Joined: Mon May 02, 2011 1:34 pm
Contact:

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

Post by glenn52 » Sat Apr 28, 2012 12:28 pm

Klaus wrote:Hi Glen
Don't you just hate it when someone can't get your name right... :wink:

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

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

Post by Klaus » Sat Apr 28, 2012 1:30 pm

Hi Glenn (sic!),

you are damn right, my friend! :D

How embarrassing... :oops:


Best

Klaus

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

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

Post by sturgis » Sat Apr 28, 2012 2:13 pm

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

Post Reply