Demonstration of the index function
#!usr/bin/perl
use warnings;
use strict;
my $string = "This is a test. Testing script is for test";
print $string, "\n";
my $foundAt = 0;
my $offset = 0;
my $label = 1;
my %positions;
while ( ( $foundAt = index( $string, 'test', $offset ) ) > -1 ) {
$positions{ $foundAt } = $label++;
$offset = $foundAt + 1;
}
foreach ( 0 .. length( $string ) - 1 ) {
print $positions{ $_ } ? $positions{ $_ } : " ";
}
Related examples in the same category