Hi,
do you know a short and easy way to iterate a block X times?
I know, one could use while, for, ... but there are some reasons,
why they don't fit.
All these versions are long. I really like Perl, because you can
do thing with shorter code. In all these loops you must define a
variable which gets increased or decreased in each iteration.
Here is the next problem. To be honest I don't know much about
how to make a program efficient, but for me it seems to be
inefficient to have a calculation, a comparison each time and two
variables (or one variable, which changes every time and a
constant) only for doing something X times. Especially if I know
how often I want to run the block before I start it and if I
don't need to know the value of $_ in
for 1..5 {
$value = $_;
}
So where is this needed?
I know Perl isn't the best language when I want to do big
calculations, but the Lucas?Lehmer primality test[1] would be one
example.
I'm not sure how this should look like. Ruby allows something
like this. So maybe one should have look there. Here a few
suggestions:
$X.times {
$s = $s * $s;
}
$X * {
$s = $s * $s
}
iterate ( $X )
{
$s = $s * $s;
}
I guess something always has to be increased/decreased if it
comes down to C/Assembly, but even if it doesn't make it faster
it would make to code smaller and easier to understand.
[1] Wikipedia also has some pseudocode:
http://en.wikipedia.org/wiki/Lucas%E2%80%93Lehmer_primality_test