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


In reply to Re: Script to reduce tokens to minimal unique characters by QM
in thread Script to reduce tokens to minimal unique characters by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.