#!/usr/bin/perl # Insensitive index/rindex written in Pure Perl # Coded by Trizen - 05 February 2012 # http://trizen.googlecode.com use strict; use warnings; sub i_index { my (@text) = split(//, lc shift(), 0); my (@comp) = split(//, lc shift(), 0); my $match_length = @comp; my @list = (0 .. $match_length - 1); foreach my $i (0 .. $#text) { if ($text[$i] eq $comp[0]) { my $x = 0; foreach my $y (@list) { $text[$i + $y] eq $comp[$y] ? ++$x : last; } return $i if $x == $match_length; } } return -1; } sub i_rindex { my (@text) = split(//, lc shift(), 0); my (@comp) = split(//, lc shift(), 0); my $match_length = @comp; my @list = (0 .. $match_length - 1); foreach my $i (reverse 0 .. $#text) { if ($text[$i] eq $comp[0]) { my $x = 0; foreach my $y (@list) { $text[$i + $y] eq $comp[$y] ? ++$x : last; } return $i if $x == $match_length; } } return -1; } my $string = 'GNU General Public License or the Artistic License'; print substr($string, i_index($string, 'art'), 6); print substr($string, i_rindex($string, 'IC '), 10);