Did I miss something?

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Did I miss something?

Post by bjb007 » Thu May 22, 2008 7:44 am

I've used code like this many times...

repeat with i = 1 to 38
put "SectorCount" & i into cTarget
if field cTarget > 2 then
put "tot" & i into NewOnes
end if
end repeat

The critical word seems to be "field" as when
cTarget is a variable I can't get it to work.

Is there a key word for a variable eg "var"
or should I use a lot of hidden fields?
Life is just a bowl of cherries.

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Post by Janschenkel » Thu May 22, 2008 12:22 pm

If the variable is a global or local variable, you can use the 'do' command to manipulate its contents indirectly.

Code: Select all

on mouseUp
  global gVar1, gVar2, gVar3
  repeat with i = 1 to 3
    put "gVar" & i into tVarName
    do "put" && tVarName && "into tArray[i]"
  end repeat
end mouseUp
Another interesting bit for this sort of work, is the 'value' function:

Code: Select all

on mouseUp
  global gVar1, gVar2, gVar3
  repeat with i = 1 to 3
    put "gVar" & i into tVarName
   put value(tVarName) into tArray[i]
  end repeat
end mouseUp

Hope this helped,

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Manipulating Variables

Post by bjb007 » Thu May 22, 2008 2:24 pm

Thanks Jan - but it's strange that a var
can't be changed directly as for a field.

Surely this would be a desirable addition to RR?

Perhaps there's a way to use a "by reference"
function like $myVar as with some languages
but I can't find one.
Life is just a bowl of cherries.

trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1005
Joined: Sat Apr 08, 2006 3:06 pm
Contact:

Post by trevordevore » Thu May 22, 2008 5:51 pm

Unforunately there isn't a syntax for using a var to reference another var. You would have to do what Jan sugested if you want to use flat vars. In cases like yours I like to use arrays. So I would convert SectorCount to an array if possible. Might look like this:

Code: Select all

repeat with i = 1 to 38 
    if SectorCount[i] > 2 then 
        put "tot" & i into NewOnes[i] 
    end if 
end repeat 
Don't know if that is possible in your case or not.
Trevor DeVore
ScreenSteps - https://www.screensteps.com

LiveCode Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode
LiveCode Builder Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode-builder

bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Fun with variables

Post by bjb007 » Sat May 24, 2008 3:25 pm

Thanks Jan and Trevor.

Haven't quite solved all my "problems" yet.

I have an array Sectors[1] etc each containing
a string of numbers like 1,2,3,5.

I want to use the individual numbers but can't figure
out a way to access them.

I can get "Sectors[34]" into theTarget but so far
can only get "Sectors[34] out!

Tried

repeat with w = 1 to 4
put item w of theTarget into xvar
put "btn" & xvar into btnTarget
set backgroundColor of btnTarget to 200,200,220
end repeat

but get nothing in xvar so obviously isn't the way to do it.

Tried a do "put" as suggested above but can't find
a combination that works.

Perhaps it just can't be done.

I could put the individual numbers into array
elements as Sectors[34,1] etc. but would be
rather long-winded and not so "elegant".

Tell me it can't be done and I'll stop trying.
Life is just a bowl of cherries.

trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1005
Joined: Sat Apr 08, 2006 3:06 pm
Contact:

Post by trevordevore » Wed May 28, 2008 12:59 pm

I can get "Sectors[34]" into theTarget but so far
can only get "Sectors[34] out!
For this technique to work you need to use the variable for the array key, not the array itself.

Code: Select all

repeat for each line theKey in the keys of Sectors
    repeat with w = 1 to 4 
        put item w of Sectors[theKey] into xvar 
        put "btn" & xvar into btnTarget 
        set backgroundColor of btnTarget to 200,200,220 
    end repeat 
end repeat
Trevor DeVore
ScreenSteps - https://www.screensteps.com

LiveCode Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode
LiveCode Builder Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode-builder

bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Post by bjb007 » Thu May 29, 2008 5:55 am

Thanks Trevor.

Seems I know most of the words
but have trouble getting them in
the right order!
Life is just a bowl of cherries.

Post Reply