in reply to How do I find the Nth occurrence of a pattern?
Here is my monkified version of the nth_iter subroutine. You gotta love map{}.
#!/usr/bin/perl -w my @string = (34, 56, 78, 90, 98, 76, 54, 32, 10, 12, 13, 16, 19, 20, +10, 56); sub nth_iter{ my ($item, $n, $list) = @_; ( map { $string[$_] == $item ? $_ : (); } 0..$#string )[--$n] or -1; } print nth_iter(78, 1, \@string);
|
---|
Replies are listed 'Best First'. | |
---|---|
RE: Answer: How do I find the Nth occurrence of a pattern?
by eak (Monk) on Aug 06, 2000 at 08:37 UTC | |
by eak (Monk) on Aug 06, 2000 at 19:20 UTC |
In Section
Seekers of Perl Wisdom