On Mon, Mar 30, 2009 at 14:20, ANJAN PURKAYASTHA
<anjan.purkayas...@gmail.com> wrote:
> Hi,
> Here is my problem;
> I have a series of arrays with 0s and 1s. here is an example: (1, 0, 1, 1).
> I need to parse through this series of arrays and extract the index of the
> 0s in the array.
> Is there any quick way of doing this?
> TIA,
> Anjan
snip

Hmm, it is an O(n) problem, so there is no quicker way than just going
through the array each element at a time.  That said, there are some
short ways of going through the array.  I would probably use map:
#!/usr/bin/perl

use strict;
use warnings;

#        0  1  2  3  4  5  6  7  8  9
my @a = (1, 1, 0, 0, 1, 0, 1, 0, 0, 0);
#so our list should be 2, 3, 5, 7, 8, and 9

my $i = -1;
my @indexes = map { $i++; $_ ? () : $i } @a;

print join(", ", @indexes), "\n";


-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to