programming-examples/perl/String/Demonstration of the index function.pl
2019-11-15 12:59:38 +01:00

23 lines
443 B
Perl

#!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{ $_ } : " ";
}