converting C to Lingo - lingo

does anyone here knows how to convert this expression below to lingo:
for(var channel=1;channel<30;channel+=3)
there is already sample below on how to use for statement to repeat with, my problem is i dont know how to use channel+=3 in lingo statement since they only provided channel++.
//Lingo
on puppetize
repeat with channel = 1 to 30
_movie.puppetSprite(channel, TRUE)
end repeat
end puppetize
// Javascript
function puppetize()
{
for(var channel=1;channel<30;channel++)
{
_movie.puppetSprite(channel, true);
}
}
hope you could help me with this. thanks.

As the Lingo reference says about the repeat keyword having no incrementing syntax, you are indeed adding 1 to channel yourself. But did you try using a more basic syntax c = c + 1 instead of c++ or c += 1? Also, in Lingo, you would be adding only 2, because the repeat loop is already adding 1 on it's own. Please see below.
//Lingo
on puppetize
repeat with channel = 1 to 30
_movie.puppetSprite(channel, TRUE)
channel = channel + 2 <---------------------my change here.
end repeat
end puppetize

Related

Mathematica Do/For loop with "Delete" or "Drop" does not work

initlNum453 = List[];
num1 = 2;
(*Pt1, initial work to make initlNum full of good variables*)
algorithmicNum = 1;
For[i7 = 1, i7 <= (num1 + 1)^2, i7++,
AppendTo[initlNum453, algorithmicNum];
If[((algorithmicNum) == (num1 + 1)), algorithmicNum = 1,
algorithmicNum++];
];
(*Pt2, delete unneeded variables*)
deleteValue = 1;
Do[
Delete[initlNum453, deleteValue];
deleteValue = (deleteValue + num1 + 2);
, {num1 + 1}
]
Here's a snippet of the code I'm trying to make (it involves pseudo-automating Lagrange polynomials). It should be simple; the first part creates a series of numbers in a list, and then the second should be delete a particular section (e.g., the 1,4,7 if n=2).
For some reason, one of the following occurs:
No Error, but the elements in the list remains the same/no elements get deleted
Taking out the semicolon says that the "Tag Times in ___ is Protected"-- can someone explain what exactly this means?
When putting this into a Module, the error states that the expression .' cannot be used as a part specification. Use Key[.`] instead.
In any case, I don't understand why something as simple as this is just doesn't work on Mathematica. The "Delete" function works outside of a for/do loop, but doesn't inside-- can someone explain why or tell me what I did wrong?
Thanks for your help! I appreciate it!
You need to write something like
initlNum453 = Delete[initlNum453, deleteValue]

I have a 'end if without block if' in visual basic when it compiles

I am having a problem when I compile my .exe file is says this is the source of the fault. Can anyone recreate this code for me?
Private Sub tmrCounter_Timer()
cntCounter = cntCounter + 1
tmrLogger.Enabled = False
SendCurrentInfos
cntCounter = 0
tmrCounter.Enabled = False
End If
End Sub
I doubt you need an If. You probably just need to remove the end if.
However you may need an If as you are incrementing a counter at top of the procedure, and later set it to 0.
my guess is that the last 3 lines inside the sub should be:
if cntCounter = 0
tmrCounter.Enabled = False
End If
it all depends on the value of cntCounter though, as it always increases in value it should be negative at the start to arrive at 0 at some time, or be altered somewhere else in your code

Employ early bail-out in MATLAB

There is a example for Employ early bail-out in this book (http://www.amazon.com/Accelerating-MATLAB-Performance-speed-programs/dp/1482211297) (#YairAltman). for speed improvement we can convert this code:
data = [];
newData = [];
outerIdx = 1;
while outerIdx <= 20
outerIdx = outerIdx + 1;
for innerIdx = -100 : 100
if innerIdx == 0
continue % skips to next innerIdx (=1)
elseif outerIdx > 15
break % skips to next outerIdx
else
data(end+1) = outerIdx/innerIdx;
newData(end+1) = process(data);
end
end % for innerIdx
end % while outerIdx
to this code:
function bailableProcessing()
for outerIdx = 1 : 5
middleIdx = 10
while middleIdx <= 20
middleIdx = middleIdx + 1;
for innerIdx = -100 : 100
data = outerIdx/innerIdx + middleIdx;
if data == SOME_VALUE
return
else
process(data);
end
end % for innerIdx
end % while middleIdx
end % for outerIdx
end % bailableProcessing()
How we did this conversion? Why we have different middleIdx range in new code? Where is checking for innerIdx and outerIdx in new code? what is this new data = outerIdx/innerIdx + middleIdx calculation?
we have only this information for second code :
We could place the code segment that should be bailed-out within a
dedicated function and return from the function when the bail-out
condition occurs.
I am sorry that I did not clarify within the text that the second code segment is not a direct replacement of the first. If you reread the early bail-out section (3.1.3) perhaps you can see that it has two main parts:
The first part of the section (which includes the top code segment) illustrates the basic mechanism of using break/continue in order to bail-out from a complex processing loop, in order to save processing time in computing values that are not needed.
In contrast, the second part of the section deals with cases when we wish to break out of an ancestor loop that is not the direct parent loop. I mention in the text that there are three alternatives that we can use in this case, and the second code segment that you mentioned is one of them (the other alternatives are to use dedicated flags with break/continue and to use try/catch blocks). The three code segments that I provided in this second part of the section should all be equivalent to each other, but they are NOT equivalent to the code-segment at the top of the section.
Perhaps I should have clarified this in the text, or maybe I should have used the same example throughout. I will think about this for the second edition of the book (if and when it ever appears).
I have used a variant of these code segments in other sections of the book to illustrate various other aspects of performance speedups (for example, 3.1.4 & 3.1.6) - in all these cases the code segments are NOT equivalent to each other. They are merely used to illustrate the corresponding text.
I hope you like my book in general and think that it is useful. I would be grateful if you would place a positive feedback about it on Amazon (direct link).
p.s. - #SamRoberts was correct to surmise that mention of my name would act as a "bat-signal", attracting my attention :-)
it's all far more simple than you think!
How we did this conversion?
Irrationally. Those two codes are completely different.
Why we have different middleIdx range in new code?
Randomness. The point of the author is something different.
Where is checking for innerIdx and outerIdx in new code?
dont need that, as it's not intended to be the same code.
what is this new data = outerIdx/innerIdx + middleIdx calculation?
a random calculation as well as data(end+1) = outerIdx/innerIdx; in the original code.
i suppose the author wants to illustrate something far more profoundly: that if you wrap your code that does (possibly many) loops (fors/whiles, doesnt matter) inside a function and you issue a return statement if you somehow detect that you're done, it will result in an effectively "bailable" computation, e.g. the method that does the work returns earlier than it would normally do. that is illustrated here by the condition that checks on data == SOME_VALUE; you can have your favourite bailout condition there instead :-)
moreover, the keywords [continue/break] inside the first example are meant to illustrate that you can [skip the rest of/leave] the inner-most loop from whereever you call them. in principal, you can implement a bailout using these by e.g.
bailing = false;
for outer = 1:1000
for inner = 1:1000
if <somebailingcondition>
bailing = true;
break;
else
<do stuff>
end
end
if bailing
break;
end
end
but that would be very clumsy as that "cascade" of breaks will need to be as long as you have nested loops and messes up the code.
i hope that could clarify your issues.

Recursively counting the number of characters in a string. (Ruby)

I need to write a recursive function that utilizes just two string methods, .empty? and .chop.
No, I can't use .length (Can you tell it's homework yet?)
So far I'm stuck on writing the function itself, I passed it the string, but I am unsure on how to recursively go through the characters with the .chop string method. Would I just have a counter? Syntax for this thing seems tricky to me.
def stringLength(string)
if string.empty?
return 0
else
.....
end
end
I wish I could put more down, but this is what I'm stuck at.
return 1 + stringLength(string.chop)
Thats your missing line. Here is an example of how this will work:
stringLength("Hello") = 1 + stringLength("Hell")
stringLength("Hell") = 1 + stringLength("Hel")
stringLength("Hel") = 1 + stringLength("He")
stringLength("He") = 1 + stringLength("H")
stringLength("H") = 1 + stringLength("")
stringLength("") = 0

how does Enumerable#cycle work? (ruby)

looper = (0..3).cycle
20.times { puts looper.next }
can I somehow find the next of 3? I mean if I can get .next of any particular element at any given time. Not just display loop that starts with the first element.
UPDATE
Of course I went though ruby doc before posting my question. But I did not find answer there ...
UPDATE2
input
looper = (0..max_cycle).cycle
max_cycle = variable that can be different every time the script runs
looper = variable that is always from interval (0..max_cycle) but the current value when the script starts could be any. It is based on Time.now.hour
output
I want to know .next value of looper at any time during the running time of the script
Your question is not very clear. Maybe you want something like this?
(current_value + 1) % (max_cycle + 1)
If, for example, max_cycle = 3 you will have the following output:
current_value returns
0 1
1 2
2 3
3 0
http://ruby-doc.org/core-1.9/classes/Enumerable.html#M003074

Resources