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
-
urbaud
- Posts: 120
- Joined: Tue Feb 24, 2009 12:10 am
Post
by urbaud » Tue Oct 20, 2009 6:21 am
Hi
I’m trying to display a declining balance in a text field, that is, the numbers would be displayed vertically in the field, with the largest number at the top of the field. For example
2000
1900
1800
1700
1600
Etc…
The following code works, but in reverse. It starts displaying the 10 on line 10 of the field and 9 on line 9 and so on. Is there a way to do what I want to do? Can the lines in the field be reversed or can they be changed to not have internal line numbers? Or is there some other control that I should use. I’ve looked through the dictionary for anything that I thought might work, but no luck.
Code: Select all
on mouseUp
put "" into fld "t1"
wait 1 second
repeat with x = 10 down to 1 step -1
wait 1 second
put x into line x of fld "t1"
end repeat
end mouseUp
I’ve tried several different iterations of this code, all with the same result. Any help would be greatly appreciated.
urbaud
-
dickey
- VIP Livecode Opensource Backer

- Posts: 118
- Joined: Wed Apr 08, 2009 11:54 pm
Post
by dickey » Tue Oct 20, 2009 7:38 am
Hello urbaud,
Done on the fly, but try this:
Code: Select all
on mouseUp
put empty into fld "t1"
put empty into theString
wait 1 second
repeat with x = 10 down to 1 step -1
wait 1 second
put theString & x & cr into theString
put theString into fld "t1"
end repeat
end mouseUp
Kind regards, Andrew
PS: I assume you want to display all the numbers in your field, and not just the last one calculated. In your example you say:
2000
1900
1800
1700
1600
you would substitute:
repeat with x = 10 down to 1 step -1
for:
repeat with x = 2000 down to 0 step -100
in the above code.
I hope that helps - Andrew
Last edited by
dickey on Tue Oct 20, 2009 9:23 am, edited 4 times in total.
-
malte
- Posts: 1098
- Joined: Thu Feb 23, 2006 8:34 pm
-
Contact:
Post
by malte » Tue Oct 20, 2009 8:34 am
Hi urbaud,
the reason why your script works in reverse is here:
As you start counting with 10, 10 will be put into the 10th line of the field. If you instead
Code: Select all
put x into line (11-x) of fld "t1"
it should work as expected. However, I wonder if sort isn't your friend in this case, as I suspect you have your data already.
All the best,
Malte
-
urbaud
- Posts: 120
- Joined: Tue Feb 24, 2009 12:10 am
Post
by urbaud » Wed Oct 21, 2009 4:13 am
Hi dickey,
Your code works for me, but because I am new to Rev I don’t understand what’s going on in the code. First, you make sure in line 2 that there is no value in variable ‘theString’. Why is this done? Isn’t a variable empty when you first create it? Then you put ‘theString’ variable, x and a carriage return (cr) into theString in line 6. The way I read line 6 is: ‘theString’ has no value and then the number in the repeat with statement is put into ‘theString’, followed by a cr, then put into theString; is there a way of explaining line 6 so I get what’s being done here. I didn’t think that you could or would have to put a cr in a repeat loop. Hopefully, you’ll be able to shed some light on this for me.
Code: Select all
on mouseUp
1.put empty into fld "t1"
2.put empty into theString
3.wait 1 second
4.repeat with x = 10 down to 1 step -1
5.wait 1 second
6.put theString & x & cr into theString
7.put theString into fld "t1"
8.end repeat
end mouseUp
urbaud
-
mwieder
- VIP Livecode Opensource Backer

- Posts: 3581
- Joined: Mon Jan 22, 2007 7:36 am
-
Contact:
Post
by mwieder » Wed Oct 21, 2009 4:20 am
I was just about to post this and then noticed that Malte may have beat me to it. But try
and see if that does the trick for you
-
dickey
- VIP Livecode Opensource Backer

- Posts: 118
- Joined: Wed Apr 08, 2009 11:54 pm
Post
by dickey » Wed Oct 21, 2009 9:42 am
Hello urbaud,
OK, I'll explain the code.
on mouseUp -- when the button is clicked
1.put empty into fld "t1"
we put the constant empty (an empty string) into field t1
2.put empty into theString
we put the constant empty (an empty string) into a variable named theString (you could call it anything really). This will contain the text string we will append to, during each repeat (loop) and later place in field t1 at the end of each repeat (loop).
3.wait 1 second
we wait one second before continuing script execution
4.repeat with x = 2000 down to 0 step -100
we commence a repeat loop by declaring the variable x with a value of 2000. This loop will continue until x=0, and at the start of each loop we will reduce the value of x by 100. The value of x starts at 2000 becomes 1900, 1900 becomes 1800, 1800 becomes 1700 and so on.
5.wait 1 second
we wait another one second before continuing script execution
6.put theString & x & cr into theString
the variable theString starts empty, but each loop we are appending the value of x concatenated (joined) with a cr (carriage return) to the previous value of the variable theString.
So Loop 1:
2000cr (the new value of variable theString)
which will apear as
2000
Loop 2:
2000cr1900cr (the new value of variable theString)
which will apear as
2000
1900
Loop 3:
2000cr1900cr1800cr (the new value of variable theString)
which will appear as
2000
1900
1800
etc etc
7.put theString into fld "t1"
we then update the display by placing the value of variable theString into field t1
8.end repeat
once the value of x = 0, the repeat loop is ended
end mouseUp -- end handler
Basically, we are appending to the same string on each iteration of repeat the recalculated value of x and using cr to push the values onto separate lines.
urbaud, I hope this clarifies the code a little more.
Kind regards, Andrew
-
urbaud
- Posts: 120
- Joined: Tue Feb 24, 2009 12:10 am
Post
by urbaud » Wed Oct 21, 2009 8:02 pm
Hi Andrew,
Thanks for the quick reply to my questions. I really appreciate you taking the time to explain the code to me. I decided to use my understanding of the code, after reading your explanation, to put down what my understanding is, and share it with you. So, I've put down two different ways to display declining numbers, vertically, in either a label or text entry field.
1. Code: put x into line x of fld "l1".
Even though the repeat loop is written: repeat with x = 5 down to 1 step -1, it will only work in reverse, i.e. displays the numbers in reverse (ascending rather than descending order). This is because the code uses the internal line numbers in each field, thus forcing the repeat loop to start at the field's line 5, then line 4, and line 3 etc. with each iteration of the loop. Using this code will only cause the loop to count in reverse. On the other hand, there is the code you gave me, below.
2. Code: put theString & x & cr into theSting
The repeat loop is written the same as the other loop, ie: repeat with x = 5 down to 1 step -1
However, this code makes Rev IGNORE the internal line numbers of the field and rather puts the counting into theString variable by appending (changing) the value of the variable on each iteration, which is then displayed in the field. To move the numbers down (descending) the field it uses a cr (carriage return) to force the next iteration of the loop to the next "line".
This solution is similar to the statement used in Visual Basic: c = c + 1 or c = c - 1, whereby the value of c is altered on each iteration, either by adding 1 to c or subtracting 1 to c. This statement can be used in For/Next and Do/Until loops in VB (at this point I have a better understanding of VB than Rev, however that's about to change). Coding in VB and now, Rev, is just a hobby-a way to keep the brain active as I enter the twilight of my years. If you have any comments after reading this, I'm all ears. I want to thank you again for providing the code and the explanation.
Kind regards, Dan
urbaud
-
mwieder
- VIP Livecode Opensource Backer

- Posts: 3581
- Joined: Mon Jan 22, 2007 7:36 am
-
Contact:
Post
by mwieder » Wed Oct 21, 2009 8:42 pm
Your understanding of the different versions of the code is spot on. Your initial version is doing, of course, exactly what you told it to do. When you started things out by placing "10" into line 10 of the field, rev helpfully inserted 9 blank lines before it. Then placing "9" into line 9 of the field replaced the blank line before the 10, etc.
Another way of achieving your desired result (combining your initial approach and Andrew's solution) would be
Code: Select all
repeat with x = 10 down to 1 step -1
put x & cr after field "t1"
end repeat
Note that the repeat loop here is very much the equivalent of Visual Basic's
-
dickey
- VIP Livecode Opensource Backer

- Posts: 118
- Joined: Wed Apr 08, 2009 11:54 pm
Post
by dickey » Thu Oct 22, 2009 12:26 am
Hello Dan,
It is always good to explain things to yourself in your own terms, to consolidate your understanding of a solution. Better still if you have some knowledge of one language, and can relate a problem in a new language (one you are learning) back to the language you already know a bit about.
Your explanations are OK, I understood what you meant.
I find myself doing exactly that with Revolution and PHP etc.
If you want to change the order from descending (count down) to ascending (count up) simply change the repeat statement to:
...as per our example counting to/from 2000 in increments of 100
Code: Select all
repeat with x = 0 to 2000 step +100
It doesn't matter whether you are 8 or 88, programming does keep the mind well engaged. Good for an emotional workout as well - from triumph to frustration and back again every 15 mins. Priceless.
Kind regards, Andrew
-
mwieder
- VIP Livecode Opensource Backer

- Posts: 3581
- Joined: Mon Jan 22, 2007 7:36 am
-
Contact:
Post
by mwieder » Thu Oct 22, 2009 12:41 am
from triumph to frustration and back again every 15 mins. Priceless.
rotfl
and so true.
-
urbaud
- Posts: 120
- Joined: Tue Feb 24, 2009 12:10 am
Post
by urbaud » Thu Oct 22, 2009 12:59 am
Hi mwieder,
Thank you for your contribution of code for my problem. I tried your line of code: put x into line (11-x) of fld "t1". It worked. It's interesting how each one of us see the solution differently. It never occurred to me to subtract x from 11. By the way, I didn't have the data, so I couldn't use the sort command as you had suggested. The calculations were made on each iteration. I used this code: put prin - (prin - totnumpmt) into printot, where prin is the amount of the loan and totnumpmt is the number of payments to be made. Printot was then placed in the fld in the repeat loop.
Also, your newest line of code: put x & cr after field "t1". I have seen the word after used in other situations. The dictionary says this about the keyword after: Used with the put command to place a value at the end of a container or chunk. The wording after fld "t1" suggests that it is put beyond the fld or container, which I know it isn't. But, how does it work in a repeat loop by putting it at the end of the field?
Here's my explanation. It's similar to Andrew's explanation. The loop starts out with 10&cr in the field. Then on the next iteration it places 9&cr at the end of the contents of the field, or I guess the chunk, so the 10&cr is replaced with the value 9&cr and on the next iteration it places 8&cr at the end, thus replacing the former value of 9&cr and so on. Is everything that's placed in a container or field considered a chunk by Rev? What would be considered a container by Rev? I couldn't find any reference to container in the dictionary, but I did find a definition of chunk: A chunk is a description of a piece of text in a longer string; so I'm guessing the descriptions of the text would be a single character, multiple characters, words, items and lines. Thank you again for your help and suggestions.
Regards, Dan
urbaud
-
urbaud
- Posts: 120
- Joined: Tue Feb 24, 2009 12:10 am
Post
by urbaud » Thu Oct 22, 2009 1:12 am
Hi Andrew,
As I was writing my comments to mwieder I received your comments. I was surprised they came so fast, as I thought it would be night, down under and you would be zzzzzzzzzing. Thank you for them. As I sit in my home office in Victoria, British Columbia, Canada I can't help but be amazed that we are communicating ACROSS the world. Me in Canada and you in Shellharbour, Australia. Amazing.
I do know that by changing the writing of the repeat loop I could have had the numbers ascend or descend, as your new line of code shows: repeat with x = 0 to 2000 step +100. Both of you have been very helpful-thanks again to both of you.
Regards, Dan
urbaud
-
dickey
- VIP Livecode Opensource Backer

- Posts: 118
- Joined: Wed Apr 08, 2009 11:54 pm
Post
by dickey » Thu Oct 22, 2009 5:30 am
Hello Dan,
Only to happy to help. You are of course 'dead right' when you point out there are numerous solutions to a single problem. I have small children. I ocassionally ask them an opinion on some coding problem they can't possibly know the answer to, only to be floored in surprise by the simplicity of their approach to the core problem.
Having been in technology a long time, people do get blase about just what an incredible and powerful thing it is that people so geographically and often so culturally displaced from one another - can so happily collaborate in real time on the web.
For me, it is the essence of the internet, and is a source of endless opportunity. I hope to visit Canada one day, and walk the Capilano suspension bridge in Vancouver.
Kind regards, Andrew
-
mwieder
- VIP Livecode Opensource Backer

- Posts: 3581
- Joined: Mon Jan 22, 2007 7:36 am
-
Contact:
Post
by mwieder » Thu Oct 22, 2009 6:30 am
Dan-
You're definitely getting the hang of things now.
Think of
as
Code: Select all
put x & cr after the text of field "t1"
because that's what it's really doing. The "the text of" part is implied, but you can put it in explicitly if you desire.
So what's happening is that every time you place some text "after" (the text of) the field it's getting added onto what's already there. Kind of like Visual Basic's
Code: Select all
strMyText = strMyText + strSomeNewText
In this case the field is the "container". I'm not sure "container" is ever authoritatively defined in the documentation - I'll have to check.
-
urbaud
- Posts: 120
- Joined: Tue Feb 24, 2009 12:10 am
Post
by urbaud » Thu Oct 22, 2009 7:53 am
Hi Andrew & mwieder,
Your response time is incredible after my postings. Andrew, I hope you're not afraid of heights, as the bridge is quite high off the canyon floor and a bit wobbly if there is a wind. I remember walking across it many years ago.
The thing I'm having some difficulty "wrapping my head around" is the fact that a chunk of text (in Rev) can be put into a variable and then more text can be put (added) to the variable BEFORE the existing text, WITHIN the existing text or AFTER the existing text. In Visual Basic, as far as I know, when you 'put' some value into a variable, it over writes the existing data in the variable. There's none of this adding before, within or after the data in VB.
My understanding of it conceptually is that it's more like an actual container. That is, if it's like a container, then one could easily add to it and place the added data where ever one wanted to in the container, hence the use of the words before, after and even replace in Rev nomenclature.
Thanks again for the help in understanding chunks and the help with my particular problem. I hope that should I encounter another problem in my future coding that I might get your assistance again.
Regards, Dan
urbaud