check whether at the least item possible

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
planix
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 47
Joined: Tue Mar 10, 2009 12:47 pm

check whether at the least item possible

Post by planix » Fri Sep 28, 2012 8:31 am

Hi,

I am using a repeat loop to iterate over a list of field names and then using an array to set up an SQL statement.

It looks something like this:

Code: Select all

repeat for each item columnName in tableColumns
         put & "'" & aDBColumns[columnName] & "'" & ","after theColumnValues
   end repeat
What I would like to do is to avoid coding the deleting of the last comma after I've been through the loop. Why? Just lazy I guess :oops:

So, is it possible to do something like?:

Code: Select all

repeat for each item columnName in tableColumns
      if columnName is last item then
         put "'" & aDBColumns[columnName] & "'" after theColumnValues
      else
         put "'" & aDBColumns[columnName] & "'" & ","  after theColumnValues 
      end if
   end repeat
This doesn't work but I somehow think it should. But, wishing doesn't make it so! :roll:

Anyone know of the/a way to do this?

cheers
****************************************
PLANIX
"Knowing anything is better than knowing nothing. But knowing that nothing ain't anything, why that's just plain nirvana."

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

Re: check whether at the least item possible

Post by Klaus » Fri Sep 28, 2012 11:06 am

Hi,

hmm, "too lazy"?
In that case you should go with:
...
delete char -1 of theColumnValues
...
I'm lazy, so I do it this way!
Every other solution will be much longer :D

But you are looking for something like htis:

Code: Select all

...
put the num of items of tableColumns into tNumColumns
repeat for each item columnName in tableColumns
      if itemoffset(columnName,tableColumns) = tNumColumns then
     ## Last item
         put "'" & aDBColumns[columnName] & "'" after theColumnValues
      else
         put "'" & aDBColumns[columnName] & "'" & ","  after theColumnValues 
    end if
end repeat
...
Best

Klaus

planix
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 47
Joined: Tue Mar 10, 2009 12:47 pm

Re: check whether at the least item possible

Post by planix » Fri Sep 28, 2012 12:30 pm

You're right!

The other way
delete char -1 of theColumnValues
is much quicker.

But, thanks for showing me how to do it the other way. It's good to know.

cheers
****************************************
PLANIX
"Knowing anything is better than knowing nothing. But knowing that nothing ain't anything, why that's just plain nirvana."

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

Re: check whether at the least item possible

Post by Klaus » Fri Sep 28, 2012 1:51 pm

planix wrote:You're right!
The other way
delete char -1 of theColumnValues
is much quicker.
Yep, just ask me things like this, I'm the father of lazyness! :D

Therefore I use functions like this:

Code: Select all

## QUOTE a string:
function q tString
  return QUOTE & tString & QUOTE
end q

## And single QUOTE, especially useful for database stuff:
function q1 tString
  return "'" & tString & "'"
end q1
8)
planix wrote:But, thanks for showing me how to do it the other way. It's good to know.
My pleasure!

planix
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 47
Joined: Tue Mar 10, 2009 12:47 pm

Re: check whether at the least item possible

Post by planix » Sun Sep 30, 2012 6:37 am

Brilliant!

I will add these to my list of functions.
****************************************
PLANIX
"Knowing anything is better than knowing nothing. But knowing that nothing ain't anything, why that's just plain nirvana."

Post Reply