Using Merge function with arrays

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
edgore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 197
Joined: Wed Jun 14, 2006 8:40 pm

Using Merge function with arrays

Post by edgore » Fri Oct 11, 2013 12:24 am

Is there a trick to being able to use the Merge function with array elements, or do I have to pull the data out of each element and put it into a variable before merging?

I looked around in the forums, but was unable to find anything.

Here is the merge I am trying to do:
the elements of $_POST are:
$_POST["system_uid"] = TESTING
$_POST["name"] = TEST ID
$_POST["system_id"] = TESTSYSTEM
$_POST["mgr_ldap_uid"] = 1234567

INSERT INTO idmap_systems (system_uid, name, system_id, mgr_ldap_uid) VALUES ('[[$_POST["system_uid"]]]','[[$_POST["name"]]]','[[$_POST["system_id"]]]','[[$_POST["mgr_ldap_uid"]]]')

And, of course, I am ending up with
INSERT INTO idmap_systems (system_uid, name, system_id, mgr_ldap_uid) VALUES (']',']',']',']')

I'm going to go ahead and just pull the elements out into variables, so this is more curiosity/a chance to learn than anything else. I mean, it totally makes sense that this would not work because of the use of square brackets by both, but I would be surprised if there were not some way to directly use the elements of an array with merge.

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

Re: Using Merge function with arrays

Post by SparkOut » Fri Oct 11, 2013 10:20 am

I came across this a long time ago using some templates I made for a project with on-rev server. From memory I think the answer is simply to add an extra square bracket either side (so you have triple brackets) but for the life of me I can't remember for certain. I will be able to check when I get home but that won't be for 10 hours or more.

edgore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 197
Joined: Wed Jun 14, 2006 8:40 pm

Re: Using Merge function with arrays

Post by edgore » Fri Oct 11, 2013 3:29 pm

Figured it out - put parentheses around the array reference and it all works just fine. So:

INSERT INTO idmap_systems (system_uid, name, system_id, mgr_ldap_uid) VALUES ('[[($_POST["system_uid"])]]','[[($_POST["name"])]]','[[($_POST["system_id"])]]','[[($_POST["mgr_ldap_uid"])]]')

is the correct template text for my issue.

Hopefully this helps someone out there down the line. I guess I should probably file a bug report on the docs to get this added as well.
Last edited by edgore on Sat Oct 12, 2013 12:20 am, edited 1 time in total.

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

Re: Using Merge function with arrays

Post by SparkOut » Fri Oct 11, 2013 4:21 pm

Hi Ed, just got home (earlier than anticipated) and the way I discovered is to put an extra space between the closing square bracket and the merge pair, ie

Code: Select all

value='[[sListingA['close_deal'] ]]' 
so in this case the value merges to the sListingA['close_deal'] array element. Seems like parentheses is a more intuitive way to ensure the right result. Glad it's working.

Post Reply