put string into multiple variables

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
Da_Elf
Posts: 311
Joined: Sun Apr 27, 2014 2:45 am

put string into multiple variables

Post by Da_Elf » Thu Nov 12, 2015 12:40 pm

this might sound silly but i want to put 0 into about 20 different variables and dont want to have to write "put 0 into var1" then another line "put 0 into var2". Id rather say something like "put 0 into va1 and var2 and var3 and so on" this will be a lot easier

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: put string into multiple variables

Post by Klaus » Thu Nov 12, 2015 1:51 pm

Hi Da_Elf,

just DO it, you lazyboy:

Code: Select all

...
  repeat with i = 1 to 20
      do ("put" && 0 && "into tVar" & i)
   end repeat
...
:D


Best

Klaus

Da_Elf
Posts: 311
Joined: Sun Apr 27, 2014 2:45 am

Re: put string into multiple variables

Post by Da_Elf » Thu Nov 12, 2015 2:56 pm

Thanks Klaus. I have to look into this "do" command. So there would be no other way of doing it? i used var1, var2, var3 as examples. What if i wanted to put 0 into "TotalSales" , "TotalSavings" , "SubTotal" , "TotalTaxes". etc etc.

wait i think i have it

Code: Select all

put "TotalSales,TotalSavings,Subtotal,TotalTaxes,etc,etc" into vars
set itemDelimiter to comma
repeat for each item myVar in vars
   do ("put" && 0 && "into " & myVar)
end repeat

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: put string into multiple variables

Post by Klaus » Thu Nov 12, 2015 3:44 pm

EX-ACTLY! :D

Da_Elf
Posts: 311
Joined: Sun Apr 27, 2014 2:45 am

Re: put string into multiple variables

Post by Da_Elf » Thu Nov 12, 2015 3:55 pm

thank you so much. You have been a help to all of us novices. i might even be able to clean up some of the code ive already done by using the "do" option. (why did i think programming a POS would be simple lol)

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: put string into multiple variables

Post by Klaus » Thu Nov 12, 2015 4:16 pm

Maybe another option could be an ARRAY?
No need to DO anything there:

Code: Select all

...
put empty into tArray
put "TotalSales,TotalSavings,Subtotal,TotalTaxes,etc,etc" into vars
repeat for each item myVar in vars
   put 0 into tArray[myVar]
end repeat
...
Only ONE variable, worth a thought :D

kelyanok
Posts: 22
Joined: Sun Feb 23, 2020 8:48 am

Re: put string into multiple variables

Post by kelyanok » Sat Mar 28, 2020 6:54 pm

hello,
i want to do something similar: if mutliple variables are set to true, then...
i tried this:

Code: Select all

if _g1True is true and _g2True is true and _g3True is true
   then
      answer "working"
   end if
or if g1True,g2True,g3True is true, then.. but nothing. thats a pretty old thread, so if someone sees it.. can you help me? thank you! :D

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: put string into multiple variables

Post by Klaus » Sat Mar 28, 2020 10:45 pm

Hi kelyanok,

just made a little test and it works as advertized!

Code: Select all

on mouseUp 
   put TRUE into _g1True
   put TRUE into _g2True 
   put TRUE into _g3True 
   if _g1True = true and _g2True = true and _g3True = true  then
      answer "working"
   end if
end mouseUp
Hint: When checking a Boolean value for TRUE, you can omit the = TRUE part.
If we do, the engine thinks we mean TRUE, so this works, too:

Code: Select all

on mouseUp 
   put TRUE into _g1True
   put TRUE into _g2True 
   put TRUE into _g3True 
   if _g1True and _g2True and _g3True then
      answer "working"
   end if
end mouseUp
Best

Klaus

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

Re: put string into multiple variables

Post by Thierry » Sun Mar 29, 2020 9:06 am

Klaus wrote: Maybe another option could be an ARRAY?
No need to DO anything there:
Only ONE variable, worth a thought :D
What Klaus said !

but even an easier way to build your array out of list:

Code: Select all

   local mySuperArray = "TotalSales,TotalSavings,Subtotal,TotalTaxes,etc,etc"
   split mySuperArray with comma as set
Take care,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

kelyanok
Posts: 22
Joined: Sun Feb 23, 2020 8:48 am

Re: put string into multiple variables

Post by kelyanok » Sun Mar 29, 2020 8:38 pm

hello Klaus!

thank you for your response!
i tried to use your code, but it didnt work! :(

so what i did first is put some code when the button "start" is pressed:

Code: Select all

on mouseUp
put FALSE into _g1True
put FALSE into _g2True
put FALSE into _g3True
to be sure that the probleme wasnt there: it is not. so ive made another variable to check if my code above wasnt working or something like that, still no. i checked if everytime the player succeed, you put TRUE into _gxTrue, i put "global _gxTrue" everywhere ect ect.. but STILL NO!! :( :(
can you help me pleaaase thank you very much.
i can maybe send you my app, say me if you want to.


Ps: by _gxTrue, i mean i put _g1True, _g2True and _g3True.

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: put string into multiple variables

Post by Klaus » Sun Mar 29, 2020 9:17 pm

Hm, are these GLOBAL variables? If yes, do you declare them in every script you use them?
I think this is different than in other programming languages.

Like this:

Code: Select all

global myGlobalVar

on mouseup
  ## do this and that with myGlobalVar
end mouseup

command my_custom_command
  ## do somethong else with myGlobalVar
end  my_custom_command

kelyanok
Posts: 22
Joined: Sun Feb 23, 2020 8:48 am

Re: put string into multiple variables

Post by kelyanok » Mon Mar 30, 2020 11:28 am

hello Klaus
well i did put global _gxTrue on every box yes.
still didnt found why. is there a way to send you my code?

thanks

kelyan

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: put string into multiple variables

Post by Klaus » Mon Mar 30, 2020 11:49 am

Hi kelyan,

OK, send the stack to klaus AT major-k.de with a short info, where to look.
Or put it online somewhere and send me the link.


Best

Klaus

Post Reply