I want to "implode" URLs, that is shorten them by replacing long and/or frequently seen substrings with single non-visible ASCII chars. Essentially, the code I have is:
my $tnt = [ "http://www." , "https://www." , }; my $implode_url = sub { my ($url) = @_; my $len = scalar(@$tnt); for( my $i = 0 ; $i < $len; $i++) { $url =~ s/$tnt->[$i]/chr($i+1)/eg; # +1 to avoid \0 } return $url; };
What I'm looking to do is, given a large list of URLs find the top 31 (chr(1) to chr(31)) most valueable substrings to populate $tnt.

Obviously, the most valuable is a measure of the substring's length x frequency within the collection.

So, given a collection of URLs (one per line), does anyone have a suggested algorithm outside of brute force (pseudo-code below)?

while( $u = <STDIN> ) { for( $i=1; $i < length($u)-1; $i++ ) { for ($j = 0; $j < length($u)-$i ; $j++ ) { $s = substr($u, $j, $i) $matches($s) += ( $u =~ m/$s/g ); } } # now loop over %matches finding top 30 of: # $matches($key) * length($key) #
Thanks in advance!

mG.


In reply to Imploding URLs by mobiGeek

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.