Really you should switch to use strict and use warnings
Here is your code rewritten in 'pure perl', with this notes:
- my @xx= $str =~ /(xxx)/g # return array with matches
- scalar @xx # how many items are in @xx
- you should add use strict your self to the script
- it is not needed to go to #arr in the loop
- I really doesn't understand what you want to say in the last paragraph, so my code is only a guess
See
perlretut for more info in first note
#! /usr/bin/perl
$str1='It is a guide to action which ensures that the military always
+obey the commands of the party.'; chomp($str1);
$str2='It is a guide to action that ensures that the military will for
+ever heed Party commands is a guide.'; chomp($str2);
@arr1=split(/\s+/, $str1);
$n=0;
%result=();
%output=();
for($i=0; $i<$#arr1-1;$i++) {
$t1="$arr1[$i] $arr1[$i+1] $arr1[$i+2]";
if(@matchs=$str2=~/($t1)/g) {
$result{@matchs}=$t1;
print $i+1,"\t\t",scalar @matchs,"\t\t\t$t1\n";
$n++;
}
if(@matchs=$str1=~/($t1)/g) {
$output{@matchs}=$t1;
print $i+1,"\t\t",scalar @matchs,"\t\t\t$t1\n";
}
}
print "\n\nTotal Matches : $n\n";
if ((sort keys %output)[0] > (sort keys %result)[0])
{
print "result has the minor value\n";
}
elsif ((sort keys %output)[0] < (sort keys %result)[0])
{
print "output has the minor value\n";
}
else
{
print "Both has the same minor value\n";
}
PD to perlmonks admins: I really hate to have add <br> in every line :( when I'm commenting or posting
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.