Main Content
For loop - checked once, or evety time? Ruby v Perl comparison and contrast Archive - Originally posted on "The Horse's Mouth" - 2010-04-07 19:18:58 - Graham Ellis
Although may aspects of Ruby are inherited (in a non-OO way!) from Perl, there are some distinct differences too; a classic for loop in Perl has its end condition checked every time around the loop, but a Ruby for loop sets up an iterator at the start, so that if something changes within the loop evaluation context, the loop does not dynamically adjust. Let me show you an example.
Here's a for loop in Ruby:
stuff = ["Tom","Dick","Harriet"]
for v in 0...stuff.length do
print "#{v+1} ... #{stuff[v]}\n"
stuff[stuff.length] = "Bob"
end
p stuff
In which the loop runs 3 times because that's the number of times that's calculated before the loop starts.
Dorothy-2:ra10 grahamellis$ ruby evalo.rb
1 ... Tom
2 ... Dick
3 ... Harriet
["Tom", "Dick", "Harriet", "Bob", "Bob", "Bob"]
Dorothy-2:ra10 grahamellis$ ]
But in Perl, using the classic from, to, step , the end condition is checked every time. So if we write the equivalent code in Perl:
@stuff = ("Tom","Dick","Harriet");
for ($v=0; $v<@stuff; $v++) {
print "$v >>> $stuff[$v]\n";
$stuff[@stuff] = "Bob";
}
print ("@stuff\n");
we get an infinite loop:
Dorothy-2:ra10 grahamellis$ perl evalo.pl
0 >>> Tom
1 >>> Dick
2 >>> Harriet
3 >>> Bob
4 >>> Bob
5 >>> Bob
6 >>> Bob
7 >>> Bob
8 >>> Bob
9 >>> Bob
etc ...
Which is right? Neither - or both - depending upon how you think of it. They're different - and if you're writing a loop which modifies its end condition in any language, you need to be sure how it will behave. Perhaps it would be better for me to suggest to you that loops that dynamically change there end point in this way are best acoided!
Some other articles
R104 - Control Structures Alternating valuses / flip-flop / toggle - example in Ruby Where does Ruby load modules from, and how to load from current directory Separating your code for easier testing, understanding and re-use; example in Ruby Finding sum, minimum, maximum and average in Python (and Ruby) Conditionals, loops and methods in Ruby - a primer with simple examples Learning to program - Loop statements such as while Learning to Program - the conditional statement (if) Muttable v immutable and implications - Ruby Finding the total, average, minimum and maximum in a program Ruby v Perl - a comparison example Assigning values to variables within other statements - Ruby Does a for loop evaluate its end condition once, or on every iteration? Multiple inputs, multiple out, ruby functions Is this number between? Does this list include? - Ruby How a for loop works Java, Perl and other languages Returning multiple values from a function call in various languages - a comparison Ruby training - some fresh examples for string handling applications Splitting data reading code from data processing code - Ruby Why do I need brackets in Ruby ... or Perl, Python, C or Java Alternative loops and conditionals in Ruby and Perl This article Passing code to procedures and yield in Ruby A short form of if ... then ... else Learning to program in Ruby - examples of the programming basics Ruby, Perl, Linux, MySQL - some training notes Ruby to access web services Ruby Programming Course - Saturday and Sunday What to do with a huge crop of apples Clean code, jump free (Example in Lua) Saying NOT in Perl, PHP, Python, Lua ... Some Ruby programming examples from our course Ruby, C, Java and more - getting out of loops for loop - how it works (Perl, PHP, Java, C, etc) A better alternative to cutting and pasting code Ruby's case - no break Equality in Ruby - == eql? and equal? Breaking a loop - Ruby and other languages 1st, 2nd, 3rd revisited in Ruby P206 - More Loops and Conditionals While, for, foreach or something else to loop. Ruby v Perl - a comparison example Perl - making best use of the flexibility, but also using good coding standards How a for loop works Java, Perl and other languages Some more advanced Perl examples from a recent course Multiway branches in Perl - the given and when syntax Alternative loops and conditionals in Ruby and Perl Are you learning Perl? Some more examples for you! A pint of Black Rat, and a lazy barman Setting a safety net or fallback value in Perl switch and case, or given and when in Perl This article Question Mark - Colon operator (Perl and PHP) Saying NOT in Perl, PHP, Python, Lua ... Ruby, C, Java and more - getting out of loops for loop - how it works (Perl, PHP, Java, C, etc) Smart English Output - via PHP and Perl ? : operator Breaking a loop - Ruby and other languages The ternary operator in Python What - no switch or case statement? Perl - redo and last without a loop P204 - Conditionals and Loops Learning to program - Loop statements such as while Learning to Program - the conditional statement (if) Showing what programming errors look like - web site pitfall Flowchart to program - learning to program with Well House Does a for loop evaluate its end condition once, or on every iteration? Increment operators for counting - Perl, PHP, C and others Are you learning Perl? Some more examples for you! This article Do not copy and paste code - there are much better ways Ternary operators alternatives - Perl and Lua lazy operators Equality and looks like tests - Perl Saying NOT in Perl, PHP, Python, Lua ... Learning to program in Perl Decisions - small ones, or big ones? Lexical v Arithemetic testing, Bash and Perl Smart English Output - via PHP and Perl ? : operator -> , >= and => in Perl Wimbledon Neck