concatenation probleme [Solved]

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
atout66
Posts: 266
Joined: Wed Feb 02, 2011 12:31 pm

concatenation probleme [Solved]

Post by atout66 » Sat Feb 27, 2016 12:24 pm

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 :wink: )

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.
Last edited by atout66 on Sat Feb 27, 2016 2:51 pm, edited 1 time in total.
Discovering LiveCode Community 6.5.2.

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: concatenation probleme

Post by Thierry » Sat Feb 27, 2016 1:53 pm

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
Last edited by Thierry on Sat Feb 27, 2016 4:19 pm, edited 1 time in total.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

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

Re: concatenation probleme

Post by SparkOut » Sat Feb 27, 2016 2:02 pm

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

atout66
Posts: 266
Joined: Wed Feb 02, 2011 12:31 pm

Re: concatenation probleme

Post by atout66 » Sat Feb 27, 2016 2:51 pm

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 :wink:
Discovering LiveCode Community 6.5.2.

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: concatenation probleme

Post by Thierry » Sat Feb 27, 2016 3:22 pm

atout66 wrote: I didn't try the array method but I guess Thierry knows about it much better than I :wink:
:)

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
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

Post Reply