in reply to output differs in perl version

I happen to have access to an old freebsd machine that still runs 5.8.8 (hasn't been updated in ages...) and I see that the output looks like stuff that you've shown in earlier posts on this same general topic:
It is a t is a is a guide s a guide a guide to guide to action uide to action ide to action de to action e to action to action which o action which action which ensures ction which ensures tion which ensures ion which ensures on which ensures n which ensures which ensures that hich ensures that ich ensures that ch ensures that h ensures that ensures that the nsures that the sures that the ures that the res that the es that the s that the that the military hat the military at the military t the military the military always he military always e military always military always obey ilitary always obey litary always obey itary always obey tary always obey ary always obey ry always obey y always obey always obey the lways obey the ways obey the ays obey the ys obey the s obey the obey the commands bey the commands ey the commands y the commands the commands of he commands of e commands of commands of the ommands of the mmands of the mands of the ands of the nds of the ds of the s of the of the party. f the party.

Well, because I first learned about the (?=...) regex construct (referred to as a "positive look-ahead assertion") when using 5.8.8, I have to assume there's something tricky and unexpected that happens when the look-ahead expression can match variable-length strings (and this was apparently fixed in 5.10).

If you really have to construct word trigrams in 5.8.8, I guess you'll have to do something other than a pure regex solution relying that sort of syntax. The easier way to do word n-grams is to use split:

my $str1 = q/It is a guide to action which ensures that the military a +lways obey the commands of the party./; my $str2 = q/It is a guide to action that ensures that the military wi +ll forever heed Party commands is a guide./; for my $str ( $str1, $str2 ) { print "=== input is [$str] ===\n"; my @words = split / /, $str; while ( @words >= 3 ) { print join( " ", @words[0..2] ), "\n"; shift @words; } print "\n"; }
Of course, if that doesn't satisfy the contrived conditions of a given homework assignment ("You must not use 'split'! And you must use Perl 5.8.8!!"), well, that's a shame. The instructor ought to know better (or shouldn't be so deliberately mean to the poor students...)

Replies are listed 'Best First'.
Re^2: output differs in perl version
by Neighbour (Friar) on Jul 04, 2011 at 11:33 UTC
    But you always can implement the algorithm in a way that does work in 5.8.8. For example like this:
    while ( $str1 =~ /^(\S+\s+)(\S+\s+\S+)(.*)$/ ) { print "$1$2\n"; $str1 = "$2$3"; $n++; }

    Alternatively, you could use $' (${^POSTMATCH} doesn't work in 5.8.8):
    while ( $str1 =~ /^(\S+\s+)(\S+\s+\S+)/ ) { print "$1$2\n"; $str1 = "$2$'"; $n++; }

      Doesn't work.