Page 1 of 1

How to include variables within a string?

Posted: Fri May 06, 2016 1:49 pm
by cooper_tim13
I have a piece of code that I am trying to run, however I am trying to include variables as part of my string. This can be found in the line

put "A" into field "Field_(position)_(lettersinword)

I would like the name of the field being referenced to change according to the value of the variables.

The two asterisked words are the variables I would like to include. How do I do this?


Code: Select all

global finalword
global lettersinword
global position
position = 0
on mouseUp
 repeat until position = lettersinword
  add 1 to position
  if char(position) of finalword = "a" then
   put "A" into field "Field_(*position*)_(*lettersinword*)
  end if
 end repeat
end mouseUp

Re: How to include variables within a string?

Posted: Fri May 06, 2016 2:16 pm
by dave.kilroy
Hi cooper_tim13 and welcome to the forum! Try mixing variables and text something like this:

Code: Select all

on mouseup
  put "sausages" into tFoo
  put "egg" into tBar
  put "I would like to eat a couple of" && tFoo && "and an" && tBar & "!" into tString
  answer tString
end mouseup

Re: How to include variables within a string?

Posted: Fri May 06, 2016 3:26 pm
by dunbarx
Hi.

You are already a programmer, I see.

Because you cannot say "position = 0". You have to "put 0 into position". This is LiveCode, not basic. Many new users do this; not to worry.

Dave's post is just what you need, though I recommend a lot of practice. Know that you can go deeper with this sort of thing, where more than one level of evaluation is required when building strings. You will find the need for this one day, and will write back for advice. For now, not to worry.

Craig Newman

Re: How to include variables within a string?

Posted: Fri May 06, 2016 10:23 pm
by cooper_tim13
Hello, thankyou for the replies. However, I am still confused as to how to fix my problem..

Would someone please write up my code again with the variables included in the string? Thankyou.

Re: How to include variables within a string?

Posted: Sat May 07, 2016 1:18 am
by Newbie4
The answer to your problem is not to embed the variables in your field name but to concatenate them together to make the field name as Dave showed you.

Play around and see if you can get it yourself. You will learn more that way.

You are almost there.

Re: How to include variables within a string?

Posted: Tue May 10, 2016 9:11 am
by dave.kilroy
Hi cooper_tim13 - you seem to be getting stuck with syntax so the following code might help (it probably won't do exactly what you want as I don't know exactly what you want to do). Please note that when I tested this I had fields named "Field_1_6" through to "Field_6_6"

A couple of things to note, try to keep use of globals down if you can (unless you are disciplined in how you name and use them they can cause hard-to-trace bugs), note the use of the 'repeat with' construct (I could have also used 'repeat for' but 'repeat with' is simpler in this case...), note how string literals are mixed with variables, and finally, note the use of brackets when constructing a field's name - what happens if you remove the brackets and do you know why?

Code: Select all

on mouseUp
    put "appeal" into finalword
    put the len of finalword into lettersinword
    
    repeat with position = 1 to lettersinword
        if char position of finalword = "a" then
            put "A" into field ("Field_" & position & "_" & lettersinword)
        else
            put "B" into field ("Field_" & position & "_" & lettersinword)
        end if
    end repeat
end mouseUp
One last thing, are you by any chance storing each instance of the letter "a" in the various fields? If so then if you use 'into' when placing the letter it will overwrite what is already in the field, if you want to add the letter to what is there you need to use 'after' which will leave what is in the field intact and add the letter after it's contents...

Kind regards

Dave
cooper_tim13 wrote:Hello, thankyou for the replies. However, I am still confused as to how to fix my problem..

Would someone please write up my code again with the variables included in the string? Thankyou.