From: "Hal E. Fulton" Date: 2003-07-11T07:37:12+09:00 Subject: Re: Ruby T-Shirt Idea ----- Original Message ----- From: "Eric Schwartz" Newsgroups: comp.lang.ruby To: "ruby-talk ML" Sent: Thursday, July 10, 2003 5:22 PM Subject: Re: Ruby T-Shirt Idea > "Hal E. Fulton" writes: > > I have little knowledge of Perl. > > > > 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? > > You're actually printing the indices of the elements which are odd. :) No... I'm printing the elements that have odd indices, which is what I meant. If I printed the indices of the elements which were odd, that would be array.each_with_index {|x,i| puts i if x % 2 == 1 } > If you want to do that in Perl: > > $i=0; foreach (@array) { print "$i\n" if $_ % 2; $i++; } > If you want to print out the odd-numbered elements themselves: > > foreach (@array) { print "$_\n" if $_ %2; } When I said the odd-NUMBERED elements, I meant the elements which are NUMBERED (indexed) by odd numbers -- not that the elements themselves should be odd. In fact, I was thinking that the elements were strings, though I didn't say that. Ahh, dynamic typing... Thanks for the Perl lesson... I can always use one. Though I'm really thinking of looking at Python instead... Cheers, Hal