use warnings;
my @iparray=("and","rat","picture","pick","pack");
my @sortedarray=sort(@iparray);
my @oparray;
my $lowerlimit=0;
my $upperlimit;
my $count=0;
my $inc=1;
my $mat=0;
my $matchedstring;
sub splitter
{
	if($mat==0)
		{
		$lowerlimit=$lowerlimit+1;
		$count=$lowerlimit;
		func();
		}
	else
		{
		$matchedstring=substr($sortedarray[$count-1],0,$mat);
		my $temp=$upperlimit;
		while($temp>=$lowerlimit)
			{
			$word=substr($sortedarray[$temp],$mat,length($sortedarray[$temp]));
			if($word eq "")
				{
				push @oparray,"\\s";
				}
			else
				{
				push @oparray ,$word;
				}
			$temp--;
			}
		print @oparray;
		if($upperlimit==scalar(@sortedarray))
			{
			print "All the words are parsed";
			last;
			}
		else
			{
			$lowerlimit=$upperlimit+1;
			$count=$lowerlimit;
			$inc=1;
			func();
			}
		}
}
sub func
{
	$base=substr($sortedarray[$count],0,$inc);
	if (defined $sortedarray[$count+1])
	{
		$match=substr($sortedarray[$count+1],0,$inc) ;
	}
	else
	{
		$count = $lowerlimit;
		$inc++;
		print $inc;
		#func();
	}
	print("\n",$base," ",$match);
	$count++;
	if($base ne $match)
		{
		if(($count-1) == $lowerlimit)
			{
			$mat=$inc-1;
			print $mat;
			splitter();
			}
		else
			{
			$inc++;
			print $inc;
			unless(($count-1) == $upperlimit)
			{
				$count=$lowerlimit;
				func();
			}				}
		}
	else
		{
		$upperlimit=$count;
		func();
		}
}
func();
