Demonstration of the index function : index « String « Perl






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

1.Using index function to check out if a string contains a sub string
2.index function returns the location of the first occurrence of a substring within a string
3.Using index function.
4.Using index to search a line repeatedly.