Page 1 of 1

check whether at the least item possible

Posted: Fri Sep 28, 2012 8:31 am
by planix
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

Re: check whether at the least item possible

Posted: Fri Sep 28, 2012 11:06 am
by Klaus
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

Re: check whether at the least item possible

Posted: Fri Sep 28, 2012 12:30 pm
by planix
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

Re: check whether at the least item possible

Posted: Fri Sep 28, 2012 1:51 pm
by Klaus
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!

Re: check whether at the least item possible

Posted: Sun Sep 30, 2012 6:37 am
by planix
Brilliant!

I will add these to my list of functions.