Passing strings with commas as arguments/parameters

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
mokogobo
Posts: 37
Joined: Wed Jun 18, 2008 6:27 pm
Contact:

Passing strings with commas as arguments/parameters

Post by mokogobo » Mon Jul 28, 2008 1:50 am

Hello there.

I'm unable to figure out a way to pass a string containing a comma as an argument to a custom handler. I wrapped up some database code and pass the query as a parameter, and sometimes the query contains a comma. When I pass this, it splits the argument at the comma and treats it as multiple arguments rather than one.

Any ideas? I've looked around for a few hours, so thought I'd ask!

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Post by Janschenkel » Mon Jul 28, 2008 6:32 am

Please post the part of your script where you're calling the custom handler - it's difficult to diagnose the problem when we don't know if you're using 'call', 'send', 'do' or just executing he custom handler directly.

Best regards,

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

mokogobo
Posts: 37
Joined: Wed Jun 18, 2008 6:27 pm
Contact:

Post by mokogobo » Mon Jul 28, 2008 7:42 pm

Janschenkel wrote:Please post the part of your script...
Here you go:

Code: Select all

on dbQueryToArray a_queryString, a_connectionId
  -- BEGIN: STRING PROBLEM WORKAROUND
  if ((the paramCount > 1) and (param(the paramCount) is not an integer)) then
    put param(1) into newQueryString
    repeat with index = 2 to (the paramCount)
      put (newQueryString & "," & param(index)) into newQueryString
    end repeat
    put newQueryString into a_queryString
    put empty into a_connectionId
  else if ((the paramCount > 1) and (param(the paramCount) is an integer)) then
    put param(1) into newQueryString
    repeat with index = 2 to (the paramCount - 1)
      put (newQueryString & "," & param(index)) into newQueryString
    end repeat
    put newQueryString into a_queryString
    put param(the paramCount) into a_connectionId
  end if
  -- END: STRING PROBLEM WORKAROUND
  
  if ((a_connectionId is not empty) and (a_connectionId is an integer)) then
    put a_connectionId into connectionId
  else
    put database["connection_id"] into connectionId
  end if
  if (connectionId is an integer) then
    put (a_queryString) into sqlQuery
    put revQueryDatabase(connectionId, sqlQuery) into sqlResultSetId
    if (sqlResultSetId is an integer) then
      put (revNumberOfRecords(sqlResultSetId)) into recordCount
      if recordCount < 1 then
        return empty
      end if
      put revDatabaseColumnNames(sqlResultSetId) into columnNames
      split columnNames by comma
      revMoveToFirstRecord(sqlResultSetId)
      repeat with resultRowIndex = 1 to recordCount
        repeat for each element columnName in columnNames
          put revDatabaseColumnNamed(sqlResultSetId, columnName) into rows[resultRowIndex, columnName]
        end repeat
        revMoveToNextRecord(sqlResultSetId)
      end repeat
      put recordCount into rows["count"]
    else
      return "zberr>The result set is invalid."
    end if
  end if
  return rows
end dbQueryToArray
Notice the "workaround" section of the code that's commented. I'd like to eliminate this if possible and pass the string only.

This is saved in the stack script. I call this from various cards throughout the stack as follows:

Code: Select all

put "SELECT id, type, name FROM my_table GROUP BY type" into sqlQuery
call "dbQueryToArray" && sqlQuery

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Post by Janschenkel » Mon Jul 28, 2008 8:41 pm

You don't have to use the 'call' command to run your handler - as long as it's in the message path, Revolution will find it for you.
Instead, just use

Code: Select all

put "SELECT id, type, name FROM my_table GROUP BY type" into sqlQuery 
dbQueryToArray sqlQuery
For more information on the message path, read this excellent article by Richard Gaskin: http://www.fourthworld.com/embassy/arti ... _path.html as well as page 109 through 122 of the Revolution User Guide.

Hope this helped,

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1005
Joined: Sat Apr 08, 2006 3:06 pm
Contact:

Post by trevordevore » Tue Jul 29, 2008 4:08 am

While you should use Jan's suggestion for your particular needs here is a little tip for times when you need to use send/call.

After assigning a value to the variable that you want to pass as a parameter, you should include the variable as part of the string you send. So rather than

Code: Select all

put "SELECT id, type, name FROM my_table GROUP BY type" into sqlQuery 
call "dbQueryToArray" && sqlQuery
you would want to do this:

Code: Select all

put "SELECT id, type, name FROM my_table GROUP BY type" into sqlQuery 
call "dbQueryToArray sqlQuery"
In the first example you are appending the value of sqlQuery to the string being passed to call. Since the string contains two commas you are actually passing three parameters to dbQueryToArray.

In the second example the variable name is passed to call. The engine treats this as one parameter and passes the value of the variable to dbQueryToArray. You could pass multiple values as well:

Code: Select all

put "SELECT id, type, name FROM my_table GROUP BY type" into sqlQuery 
put 12 into theConnectionID
call "dbQueryToArray sqlQuery, theConnectionID"
Using this technique you can safely pass values when using send and call.
Trevor DeVore
ScreenSteps - https://www.screensteps.com

LiveCode Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode
LiveCode Builder Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode-builder

mokogobo
Posts: 37
Joined: Wed Jun 18, 2008 6:27 pm
Contact:

Post by mokogobo » Tue Jul 29, 2008 5:37 pm

Thank you to both Janschenkel and trevordevore. I appreciate your support quite greatly! I'm finding that the Revolution community is generally very friendly and helpful--among the most so of any development communities I've been around.

Post Reply