#!/usr/bin/perl use strict; use warnings; my $string = "PHAGEPHAGEPQKREPHAGEPWSQEPHAGEPRDLEPHAGE"; my $substring = "PHAGE"; my $count = 0; my $last_pos = -1; NAKED: { $last_pos = index( $string, $substring, $last_pos + 1 ); last if $last_pos == -1; print "Found $substring at $last_pos\n"; $count++; redo; } print "Found $count instances of $substring\n"; __OUTPUT__ Found PHAGE at 0 Found PHAGE at 5 Found PHAGE at 15 Found PHAGE at 25 Found PHAGE at 35 Found 5 instances of PHAGE #### #!/usr/bin/perl use strict; use warnings; my @array = split //, "PHAGEPHAGEPQKREPHAGEPWSQEPHAGEPRDLEPHAGE"; my @subarray = split //, "PHAGE"; my $offset = 0; my $count = 0; NAKED: { foreach my $index ( 0 .. $#subarray ) { next if $array[ $offset + $index ] eq $subarray[ $index ]; $offset += 1; redo NAKED; } print "Found @subarray at $offset\n"; $count++; $offset += @subarray; last if $offset + @subarray > @array; redo; } print "Found $count instances of @subarray\n"; __OUTPUT__ Found P H A G E at 0 Found P H A G E at 5 Found P H A G E at 15 Found P H A G E at 25 Found P H A G E at 35 Found 5 instances of P H A G E