This is linked to another question/code-golf i asked on Code golf: "Color highlighting" of repeated text
I\'ve got a file \'sample1.txt\' with the following co
Straightforward with Perl:
#! /usr/bin/perl
use warnings;
use strict;
my @words = qw/
LoremIpsum
LoremIpsu
dummytext
oremIpsum
LoremIps
dummytex
industry
oremIpsu
remIpsum
ummytext
LoremIp
dummyte
emIpsum
industr
mmytext
/;
my $to_replace = qr/@{[ join "|" =>
sort { length $b <=> length $a }
@words
]}/;
my $i = 0;
while (<>) {
s|($to_replace)|++$i; "$1 "|eg;
print;
}
Sample run (wrapped to prevent horizontal scrolling):
$ ./tag-words sample.txtLoremIpsum issimplydummytext oftheprintingandtypesettingindus try .LoremIpsum hasbeentheindustry 'sstandarddummytexteversincethe1500s,whenanunknownprintertookagalleyoftypeandscrambledittomakeatyp especimenbook.
You may object that all the qr//
and @{[ ... ]}
business is on the baroque side. One could get the same effect with the /o
regular-expression switch as in
# plain scalar rather than a compiled pattern
my $to_replace = join "|" =>
sort { length $b <=> length $a }
@words;
my $i = 0;
while (<>) {
# o at the end for "compile (o)nce"
s|($to_replace)|++$i; "$1 "|ego;
print;
}