my @arr = qw(cat dog pig horse donkey); for my $line (@arr) { my $index = (1=~//..1=~/0/)-1; print "$index = $line\n"; }