Page 1 of 1
concatenation probleme [Solved]
Posted: Sat Feb 27, 2016 12:24 pm
by atout66
Hi to all,
I don't remember how LC evaluate the construction of a dynamic variable ?
For example I've 4 var called lesWEB1 , lesWEB2 , lesWEB3 , lesWEB4
I want to build one list of those which are not empty (of course

)
Code: Select all
repeat with xx = 1 to 4
if lesWEB & xx <> empty then -- OK until here
put (lesWEB & xx) into laListe -- doesn't work ! laListe = lesWEB1 for example instead of the values of lesWEB1
--put lesWEB & xx into laListe -- doesn't work either...
put laListe & "/" after laListeTotale
end if
end repeat
delete last char of laListeTotale -- the itemDelimiter
Any idea what's wrong in my code ?
Thanks in advance, Jean-Paul.
Re: concatenation probleme
Posted: Sat Feb 27, 2016 1:53 pm
by Thierry
atout66 wrote:
Thanks in advance, Jean-Paul.
Bonjour Jean-Paul,
This one should work (not tested)
Code: Select all
repeat with i = 1 to 4
do "get lesWEB" & i
if IT is not empty then \
put IT & slash after laListeTotale
end repeat
delete last char of laListeTotale
Or using arrays:
Code: Select all
repeat for each key K in lesWEB
if lesWEB[ K] is not empty then \
put lesWEB[ K] & slash after laListeTotale
end repeat
delete last char of laListeTotale
Or:
Code: Select all
combine lesWEB by slash
put replaceText( lesWEB,"(?<=/)/+|/$",empty)
or:
I'm sure there are some more...
Best,
Thierry
Re: concatenation probleme
Posted: Sat Feb 27, 2016 2:02 pm
by SparkOut
The concatenation works, but that provides a literal name for a variable, not a reference to the variable itself. Try a small change:
Code: Select all
repeat with xx = 1 to 4
if value(lesWEB & xx) <> empty then
put value(lesWEB & xx) into laListe
put laListe & "/" after laListeTotale
end if
end repeat
delete last char of laListeTotale -- the itemDelimiter
value as a function forces evaluation of the literal provided to it.
Of course it would be much easier to use an array than sequentially numbered variable names.
Code: Select all
repeat with xx = 1 to 4
if lesWEB[xx] <> empty then
put lesWEB[xx] into laListe
put laListe & "/" after laListeTotale
end if
end repeat
Re: concatenation probleme
Posted: Sat Feb 27, 2016 2:51 pm
by atout66
Thanks to Thierry and SparkOut for your help.
I tried the do "get xxx" method from Thierry and the value() method from SparkOut and it's exactly what I was looking for.
I didn't try the array method but I guess Thierry knows about it much better than I

Re: concatenation probleme
Posted: Sat Feb 27, 2016 3:22 pm
by Thierry
atout66 wrote:
I didn't try the array method but I guess Thierry knows about it much better than I

That's what arrays are for.
You need to declare and use only one variable.
The code with arrays is, to my humble and personal opinion, much more elegant.
If you have to run your script once in a while, it doesn't really matter,
but if you have to run it very often and in critical time context,
then arrays will speed up your code.
Bon week-end,
Thierry