concatenating with the ampersand

Kay C Lan lan.kc.macmail at gmail.com
Sun Mar 11 21:16:45 EDT 2007


On 3/12/07, john at debraneys.com <john at debraneys.com> wrote:
>
> put word 1 of the clickLine into tWhichLine
> put ( tWhichField & "_" & tWhichLine) into gStartedHere
>
> rather than
>
> put (the short name of the target & "_" & word 1 of the clickLine) into
> gStartedHere


Jim put me onto the 'merge()' function, check it out in the docs. Actually
I'm surprised it hasn't already mentioned here as it pretty well eliminates
the need for & or && in most cases.

put merge("[[the short name of the target]]_[[word 1 of the clickLine]]")
into gStartedHere

As for the initial problem, I wouldn't use merge() for this but it could be:
put merge("[[quote]][[lineNum]]H[[quote]]") into fldName

When initially used the double square brackets can be a little confusing but
once you're use to it it can make code more readable and compact.

put "Name1:" & tab & var1 & "." & cr \
    & "Name2:" & tab & var2 & "." & cr \
    etc etc
    & "Name10:" & tab & var10 & "." \
    into field "listsVars"

put merge("Name1:[[tab]][[var1]].[[cr]] \
           Name2:[[tab]][[var2]].[[cr]] \
           etc etc
           Name10:[[tab]][[var10]].") \
           into field "listsVars"

I generally find it easiest to just write it the way I want it (no spaces
unless required), then put the square brackets around what needs to be
evaluated into a string:

Namea:tabvara.cr
Name[[a]]:[[tab]]var[[a]].[[cr]]

Of course the other beauty of merge() is that merge() inside a merge()
allows you to build dynamic variables and evaluate them, such as in a repeat
loop. The following does the same as above, just a lot less code.

repeat for each item a in listOfTen
put merge("Name[[a]]:[[tab]][[" & merge("[[var&a]]") & "]].[[cr]]") after
field "listsVars"
end repeat

HTH



More information about the use-livecode mailing list