Adding Card Fields to an Array

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
townsend
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 430
Joined: Sun Feb 13, 2011 8:43 pm

Adding Card Fields to an Array

Post by townsend » Sun Jun 12, 2011 5:50 pm

Rather than hard coding all my SQL statements into a variable like tSQL,
I want to store them in an array, which will make editing them easier.

This code works fine in the Message Box.
It creates a keyed entry into the array, SQL.

Code: Select all

put "SELECT * FROM control" into SQL["select-all"]
Now I have two fields on my card: actualSQL & SQLkey.

When I take the same code and put it in a button like this:

Code: Select all

on MouseUp
    put field actualSQL into SQL[field SQLkey]
end MouseUp
I get an execution error on syntax.

So then I try:

Code: Select all

put actualSQL into act
put SQLkey into ky
put act into SQL[ky]
Then the object names are inserted. So I think it needs something like
"value of actualSQL" or "text of SQLkey" but these don't work.

I've looked in the User Guide, the Dictionary and through Array Lessons.
What am I missing?

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: Adding Card Fields to an Array

Post by SparkOut » Mon Jun 13, 2011 12:11 am

Very probably quotes, and a reference to the control identifier. And quite possibly "value" but not quite sure whether I have understood correctly, which likely I haven't. Does this work?

Code: Select all

put field "actualSQL" into SQL[(value(field "SQLkey"))] 
Maybe just using : the text of field "SQLkey" in the right place would work.

BvG
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1239
Joined: Sat Apr 08, 2006 1:10 pm
Contact:

Re: Adding Card Fields to an Array

Post by BvG » Mon Jun 13, 2011 12:05 pm

you probably want something like this:

Code: Select all

put field "actualSQL" into mySQLArray[field "actualSQL"]
and that should work. If it doesn't most likely the fields do not exist, or there's a type somewhere.
Various teststacks and stuff:
http://bjoernke.com

Chat with other RunRev developers:
chat.freenode.net:6666 #livecode

townsend
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 430
Joined: Sun Feb 13, 2011 8:43 pm

Re: Adding Card Fields to an Array

Post by townsend » Mon Jun 13, 2011 4:54 pm

Thanks guys-- I appreciate the help.

I finally figured out what the problem was. It wasn't syntax.
User Guide PDF wrote:Important: You must also use the global command at the start of a handler to make
an existing global variable available to a handler. While a global variable can be used by
any handler, you must do this in any handler you are going to use it in. If you don't
declare a global variable before using it, the handler will not take the global variable's
existence into account, and will simply create a local variable with the same name.
This means-- if you're going to use Global myVariable, you're to need to put
that statement,Global myVariable, in every handler it is referenced in.

With this in mind, I'm not even going to bother defining them at the top
of the Stack or Card Scripts. I'll just put then where they are needed.

A Custom Property on the other hand could be defined in one place
and referenced from anywhere without redefinition.

BvG
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1239
Joined: Sat Apr 08, 2006 1:10 pm
Contact:

Re: Adding Card Fields to an Array

Post by BvG » Mon Jun 13, 2011 6:33 pm

actually, you can define variables and constants outside of handlers, in every script. That is the only thing that can exist outside a handler and still work. for example:

Code: Select all

global globX
constant constOffset = "42"

on mouseUp
  put globX + constOffset
end mouseUp
Various teststacks and stuff:
http://bjoernke.com

Chat with other RunRev developers:
chat.freenode.net:6666 #livecode

townsend
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 430
Joined: Sun Feb 13, 2011 8:43 pm

Re: Adding Card Fields to an Array

Post by townsend » Mon Jun 13, 2011 6:58 pm

Excellent point! So if I have a script with a dozen handlers,
all I need to do is put the Globals, and Constants at the top.

Post Reply