ForNext
Only Do What Only You Can Do
110. indexed iteration
Hyperpolyglot Interpreted Languages: PHP, Perl, Python, Ruby より (2012.05.08)
VBScript
JScript
Perl
none; use range iteration from 0 to $#a and use index to look up value in the loop body
PHP
$a = array("do", "re", "mi" "fa"); foreach ($a as $i => $s) { echo "$s at index $i\n"; }
Python
a = ['do', 're', 'mi', 'fa'] for i, s in enumerate(a): print('%s at index %d' % (s, i))
Ruby
a = %w(do re mi fa) a.each_with_index do |s,i| puts "#{s} at index #{i}" end