Executing Rev statements in a variable

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
phaworth
Posts: 592
Joined: Thu Jun 11, 2009 9:51 pm

Executing Rev statements in a variable

Post by phaworth » Wed Oct 21, 2009 6:01 am

The subject is what I want to do. Looks like the do command is the answer.

How are statements separated - return?

Do If statements count as 1 statement or as the number of statements they control?

Are there other ways to do this rather than the do command?

Thanks,

Pete

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

Post by Janschenkel » Wed Oct 21, 2009 6:25 am

Statements are normally return-separated (though I believe you can also use semicolons to concatenate multiple command onto the same line - but these will still be counted as separate statements.) If my memory hasn't gone astray, then control structures such as (if...end if) and (try...catch...end try) count as 1 towards the scriptLimits.
Depeding on what you want to do, you can solve your problems with a smart combination of the do command, value function, behaviors, frontScripts, library stacks or backScripts - so the main question to provide you with a good answer: what are you trying to accomplish?

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

phaworth
Posts: 592
Joined: Thu Jun 11, 2009 9:51 pm

Post by phaworth » Wed Oct 21, 2009 6:43 am

Thanks Jan. Yes I should have explained more abut what I'm trying to achieve.
At the risk of you telling me I should buy your product, I'm trying to put together a simple report writer for users to define their own reports.

The report definitions have placeholders in them for data to be retrieved from from an SQLite database. The placeholder resolve to anything from simple retrieval of a value from a column in the current table entry to more complex arithmetic and conditional statements.

I have a repeat loop which looks for each placeholder in a report definition line and replaces it with the database value.

There is a table that holds the definitions of each placeholder, so each time I find a placeholder in the report definition, I look it up in the placeholder table and then need to execute the definition for it. so for example, the definition might be a revDatabaseColumNamed statement at it's simplest up to an if...then...else statement or some arithmetic statements.

Hope that's clearer.

Thanks,

Pete

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

Post by Janschenkel » Wed Oct 21, 2009 8:03 pm

You may be interested in the merge function as it allows you to evaluate placeholders, including pieces of if-then-else script, and as far as I know, there's no real script limit to them.
Example:

Code: Select all

First name: [[tFirstName]]
Last name: <? return tLastName ?>
<? if tComments is not empty then return "Comments:" && tComments
else return "No comments" ?>
Assuming you stored this in a custom property named 'uTemplate' then your script could look something like:

Code: Select all

on mouseUp
  local tFirstName, tLastName, tComments
  put "John" into tFirstName
  put "Doe" into tLastName
  put "No idea what his real name is..." into tComments
  answer merge(the uTemplate of me)
end mouseUp
The merge function is extremely versatile. See also these articles in the revUp newsletter that I wrote a while back: Ever since Richard Gaskin introduced me to it way back when, I've been using merge to put together SQL queries to fetch stuff from databases, AppleScript and VBScript to automate other applications, etc.

HTH,

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

phaworth
Posts: 592
Joined: Thu Jun 11, 2009 9:51 pm

Post by phaworth » Wed Oct 21, 2009 8:22 pm

Wow! Thanks a lot Jan, this looks very interesting. I just read two of the three articles and really like the MsWord merge technique. Up to now I've been using the Word mail merge function in conjunction with a csv file to do this sort of thing but this looks easier. I'm particularly interested in how to deal with a variable number of lines and will have to research that.

I have managed to get the do command working on relatively simple statements. Appears that a semicolon is the separator between command.

Thanks again.

phaworth
Posts: 592
Joined: Thu Jun 11, 2009 9:51 pm

Post by phaworth » Mon Nov 02, 2009 8:26 pm

Trying to use the merge command as documented in Jan's links above. Here's the code I'm using:

Code: Select all

put revMacFromUnixPath(specialFolderPath("Documents") & "/Bandtrak/Report Templates/Contract.doc") into theFile
   put the printContract of me into theAppleScript
   merge (theAppleScript) 
   do theAppleScript as "AppleScript"
I split the merge and do commands because nothing was happening when i used "do merge". Now I'm getting an error "(Handler: can't find handler) near merge" on the merge statement.

printContract is the customProperty of the object the code executes in and contains:

Code: Select all

tell application "Microsoft Word"
open [[theFile]]
end tell
Any ideas?

phaworth
Posts: 592
Joined: Thu Jun 11, 2009 9:51 pm

Ignore the last post

Post by phaworth » Mon Nov 02, 2009 8:40 pm

Never mind, just realised mege is a function!
Pete

phaworth
Posts: 592
Joined: Thu Jun 11, 2009 9:51 pm

Post by phaworth » Tue Nov 03, 2009 2:00 am

I think I have a real problem this time!

Some of the placeholders in the document I'm merging into are not being replaced. I've checked and double checked the syntax of the placeholders and that the variables are correctly named and have the correct contents in my code but can't spot anything obvious.

Here's some of the rtf file that I'm having the problem with:

Code: Select all

[[SigAddress1]][[SigAddress2]]
[[SigCity]],  [[SigState]] [[SigPostCode]]
[[SigCountry]]
After the merge, it looks like this:

Code: Select all

[[SigAddress1]][[SigAddress2]]
[[SigCity]],  CA 95472
USA
Using the debugger, I checked the variables right before the merge and they all have the correct name and valid contents.

Somehow or other the SigState, SigPostCode, and SigCountry placeholders work OK but the others don't.

Any ideas?

Pete

phaworth
Posts: 592
Joined: Thu Jun 11, 2009 9:51 pm

Post by phaworth » Tue Nov 03, 2009 7:12 pm

Been looking into this further and I believe it has something to do with the use of tables. The placeholders were in one cell of a table. I replaced the table with a 2-column layout and now everything works fine with exactly the same placeholders and the same positioning of them.
Pete

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”