in reply to Script to reduce tokens to minimal unique characters

A quick hack to find all of the unique token abbreviations, construct a regex, and report the matching tokens.
#!/usr/bin/env perl use strict; use warnings; my @tokens = qw/report_time report_day reset read/; my @abbrev; for my $token (@tokens) { if (1 == grep /^$token/, @tokens) { push @abbrev, $token; } else { die "Error: $token is not unique in list of tokens\n"; } # Generate abbreviations that match only one token my $abbrev = $token; while (1) { chop($abbrev); last unless (1 == grep /^$abbrev/, @tokens); push @abbrev, $abbrev; } } # Show the abbreviations print "Abbreviations = (" . join(',', sort @abbrev) . ")\n"; # Create the regex string to use my $regex = join('|', sort @abbrev); print "Token regex = \"$regex\"\n\n"; # Read the file and output tokens while (<>) { my $match; if (($match) = m/^($regex)$/) { my @matches = grep /^$match/, @tokens; print "$match matched for @matches on line $.\n"; } } exit;

Input file:

report_t 14:09:33 PDT report_d Fri Jun 12 2015 res Resetting the time report_time 00:00:00

Output:

Abbreviations = (rea,read,report_d,report_da,report_day,report_t,repor +t_ti,report_tim,report_time,res,rese,reset) Token regex = "rea|read|report_d|report_da|report_day|report_t|report_ +ti|report_tim|report_time|res|rese|reset" report_t matched for report_time on line 1 report_d matched for report_day on line 3 res matched for reset on line 6 report_time matched for report_time on line 9

There's probably a module or two that will do this for you, but I didn't bother to look for it.

Edit to add:

I also thought of creating regex expressions, as above, but with a single subexpression per unique token. For report_time, the regex is:

m/^report_t(?:i(?:m(?:e)?)?)?/

It's a bit more complicated to do this than the above script's method. For a small list of @tokens, it makes little difference. For huge lists, it's probably better to put the original pipecleaner version through an optimizer, which will be somewhat better than this head-scratching fingernails version.

-QM
--
Quantum Mechanics: The dreams stuff is made of