Apply Multiplication Across Points List Using Repeat

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
AZSun
Posts: 30
Joined: Tue Mar 20, 2012 10:44 pm

Apply Multiplication Across Points List Using Repeat

Post by AZSun » Fri Mar 28, 2014 6:13 pm

I'm creating a drawing of a concave hexagon built with three pieces. >_< (imagine the bar on top) I created a points list that gives me the first of three parts. I believe if I multiply the first item in each line of the points list by (-1) it will give me the mirror image of the graphic object. This will give me part three that I'll move into position later. (Also, I'll worry about part two/the middle bars later)
It's a short list, so I know I could tear it apart and do the math for each x coordinate. But, I want to practice using repeat. It has been a while since I've used it and I don't know what kind of repeat I should use. My points list looks like this:
44,0
0,0
44,44
0,88
44,88
I've tried using repeat with chunk expressions to identify the first item of each line.
My attempt so far: It repeats 5 times like I want, but only for item 1 of line 1 yielding five copies of -44. So, it's doing what I'm telling it. It's just not doing what I want:) To fix this, I need help to figure out how to move/step to the next line in turn. Also, I'll have to assemble the x's and y's back into a points list.

Code: Select all

 set the itemdelimiter to comma
   Put tLeftGutterPointsList into temp
   repeat with i = 1 to the number of lines in temp
      put item 1 of line 1 of temp * (-1) after tRightGutterPointsList
   end repeat
   put tRightGutterPointsList into fld "RightGutterPointsList"
I've read in the dictionary repeat and multiplication can be used on arrays, but don't know much about this. Since I'm multiplying a points list is it better to make an array?

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Apply Multiplication Across Points List Using Repeat

Post by Mark » Fri Mar 28, 2014 6:21 pm

Hi,

Repeat for each is very fast, but arrays tend to slow it down. If you're using a simple points list, don't use an array.

Code: Select all

set the itemdelimiter to comma
put tLeftGutterPointsList into temp
repeat with i = 1 to the number of lines of temp
   put -item 1 of line i of temp & comma after tRightGutterPointsList
end repeat
put char 1 to -2 tRightGutterPointsList into fld "RightGutterPointsList"
Just replace 1 with i. Make sure to add the comma to create the new list. You can but the sign before the item keyword. When putting the list into a field, you don't want to copy the last comma, hence -2.

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

AZSun
Posts: 30
Joined: Tue Mar 20, 2012 10:44 pm

Re: Apply Multiplication Across Points List Using Repeat

Post by AZSun » Fri Mar 28, 2014 6:37 pm

Thank you for the advice about avoiding an array for such a small list. I'll work on your strategy for moving through a repeat loop.

AZSun
Posts: 30
Joined: Tue Mar 20, 2012 10:44 pm

Re: Apply Multiplication Across Points List Using Repeat

Post by AZSun » Fri Mar 28, 2014 7:05 pm

Thank you so much! I see the key part

Code: Select all

put -item 1 of line i of temp
the ...of line i...does the work of stepping through each line of the list. Can the multiplication be made clearer? Is this a text replacement or is this multiplication? It did give me: -44,0,-44,0,-44 the expected values. (-1)*44= -44 and (-1)*0=0, but can I do multiplication by some other number in this manner? I'm just having a bit of trouble seeing how that part works.

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

Re: Apply Multiplication Across Points List Using Repeat

Post by Klaus » Fri Mar 28, 2014 8:42 pm

I think it should read "put item -1 of line i of ...", which defines the LAST item of that line.
Yes, LC also lets us count from the end :D

If you whant real SPEED, use "repeat for each":

Code: Select all

...
set the itemdelimiter to comma
put tLeftGutterPointsList into temp
repeat for each line tLine in temp
   put -item 1 of tLine & comma after tRightGutterPointsList
end repeat
put char 1 to -2 tRightGutterPointsList into fld "RightGutterPointsList"
...
Hint: "repeat for each" is "read only", means you cannot modify tLine in that case!

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Apply Multiplication Across Points List Using Repeat

Post by Mark » Fri Mar 28, 2014 8:43 pm

Hi,

Honestly, I don't know how LiveCode does that internally. I'm too busy with other things to actually look at LiveCode's engine code. The trick with the minus sign before the chunk reference only works with -1. If you want to use -2, you'll need to multiple like

Code: Select all

 put -2*(item 1 of line i of temp)
and that's the same type of syntax which you already had before.

Klaus: no, you're wrong, I used a trick. (But you're right about the repeat for loop, I mentioned it, but didn't give the example).

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Apply Multiplication Across Points List Using Repeat

Post by dunbarx » Fri Mar 28, 2014 10:18 pm

Mark.

Did you mean Klaus was wrong about:

put -item 1 of tLine & comma after tRightGutterPointsList

and that you used a trick? Because that is not valid syntax. Or was it some other part of his post? If so, what was the trick?

Craig

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Apply Multiplication Across Points List Using Repeat

Post by Mark » Fri Mar 28, 2014 10:47 pm

Craig,

Have you tried it?

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Apply Multiplication Across Points List Using Repeat

Post by dunbarx » Fri Mar 28, 2014 11:34 pm

Mark.

Well, so I did just now, and get a syntax error:

button "Button": execution error at line 4 (Operators -: error in right operand), char 4

(for: put -item 1 of line i of temp & comma after tRightGutterPointsList

Just like I thought I would. What have you got?

Craig

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Apply Multiplication Across Points List Using Repeat

Post by Mark » Fri Mar 28, 2014 11:38 pm

Funny, Craig. It just works here:

Code: Select all

on mouseUp
   put "1,2,3" into tLine
   put -item 1 of tLine & comma after tRightGutterPointsList
   put char 1 to -2 of tRightGutterPointsList
end mouseUp
and OP also says that it works for him, as he says he's getting the expected results.

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

AZSun
Posts: 30
Joined: Tue Mar 20, 2012 10:44 pm

Re: Apply Multiplication Across Points List Using Repeat

Post by AZSun » Sat Mar 29, 2014 12:35 am

Klaus, thanks for the additional information. If I need it later to work on the Y coordinate, working backwards might be nice. I will also try the repeat for each method you describe. I like practicing with tasks and purposes that have attracted my interest. I did the draw line graph tutorial just prior to this to attempt to get a better handle at drawing.

Mark, thanks for the shortcut of -item… it does look like a one trick pony. (cool by the way.) Earlier today after my follow-up question I did try using that pattern but putting in other numbers, but then it fails. -item works to multiply by (-1) but -2item errors out. I'll experiment with the example you just gave for multiplication. With that sample it looks like I could also add numbers to the first item if I do things right.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Apply Multiplication Across Points List Using Repeat

Post by dunbarx » Sat Mar 29, 2014 4:53 am

Oh,

You had a number. Didn't see the tree, just looked at the forest.

- someNumericalReference

works just fine

I thought I was looking at:

- anyStringAtAll

Craig

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

Re: Apply Multiplication Across Points List Using Repeat

Post by Klaus » Sat Mar 29, 2014 12:07 pm

Hi Mark,
Mark wrote:...Klaus: no, you're wrong, I used a trick...
yep, got it!

I ususally put parens around stuff like this, because I am a very visual guy :D
...
put -(item 1 of line i)...
...


Best

Klaus

AZSun
Posts: 30
Joined: Tue Mar 20, 2012 10:44 pm

Re: Apply Multiplication Across Points List Using Repeat

Post by AZSun » Sat Mar 29, 2014 4:27 pm

I got a chance to try the "repeat for each" pattern and it worked well, but I had to pop over into the dictionary to figure out why. The tLine variable just showed up in the sample you gave… I changed it to i (I know this makes no real difference) then I could more easily see this was the labelVariable/iterative variable?/the thing that makes it work.

I also figured out the shift and recombining with the y coordinate.
repeat for each line i in temp
put -(item 1 of i) +100 & comma & (item 2 of i) & cr after tRightGutterPointsList
end repeat
I'm going to try the repeat array just for practice.
Thanks,
Eric

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

Re: Apply Multiplication Across Points List Using Repeat

Post by Klaus » Sat Mar 29, 2014 5:11 pm

Hi Eric,

with using "repeat for each", the engine does NOT count the lines/items etc. in every loop,
which it DOES when using "repeat with".

That's why this is manyfold faster, especially when the number of lines/items etc. grows,
means the bigger the data, the faster "repeat for each" :D

"repeat for each line tLine" will give you the CONTENT of the current line in tLine (or i in your case)
in contrary to "repeat with" whre it "only" hands you the current NUMBER.


Best

Klaus

Post Reply