On Thursday 10 July 2003 23:42, Hal E. Fulton wrote:

> Suppose you wanted to iterate through an array and
> print out the odd-numbered elements:
>
>   array.each_with_index {|x,i| puts x if i % 2 == 1 }
>
> How would that look in Perl?

That's a good intent because Perl lacks an explicit construct to iterate 
with both the index and the element, and does not come with a  puts() 
in addition.

For instance we could write

    $_ % 2 and print "$array[$_]\n" foreach 0..$#array;

If one considers it's bad style to use boolean operators to control flow 
that way, we could write instead

    print "$array[$_]\n" foreach grep $_ % 2, 0..$#array;

or plain

    foreach (0..$#array) { 
        print "$array[$_]\n" if $_ % 2;
    }

Note that I have not used "for", which is wholly interchangeable with 
"foreach", to shorten the snippets. I prefer to write foreach when I 
mean foreach.

I woldn't be proud wearing a T-shirt that compared Ruby to any other 
language that way. Both Ruby and Perl have their place, I love both.

-- fxn