Do you know of any module that can achieve this with quite long hashes?

update 2: the solution below the 1th update doesn't weed out the key 'technlogy' which is found as partial key in 'automation technology' and 'automation technology process'. B_TREE partial key matching isn't useful, since the match starts at the beginning of the key. DB_File is good to store huge simple hashes and quite fast, so this statement remains.

Here's a solution:

use strict; use warnings; use Data::Dump; my %h = ( 'rendition' => '3', 'automation' => '2', 'saturation' => '3', 'mass creation' => 2, 'technology' => 5, 'process' => 6, 'creation' => 7, 'saturation process' => 9, 'automation technology' => 2, 'automation technology process' => 3, ); dd 'before', \%h; for (keys %h) { if (/\s/) { my @l = split ' '; for my $k (map {my $i=$_;map{join" ",@l[$i..$_]}$i..$#l}0..$#l +) { delete $h{$k} unless $k eq $_; } } } dd 'after', \%h; __END__ ( "before", { "automation" => 2, "automation technology" => 2, "automation technology process" => 3, "creation" => 7, "mass creation" => 2, "process" => 6, "rendition" => 3, "saturation" => 3, "saturation process" => 9, "technology" => 5, "technology process" => 5, }, ) ( "after", { "automation technology process" => 3, "mass creation" => 2, "rendition" => 3, "saturation process" => 9, }, )

Skip the rest of this reply.

update: wrong, scratch hat, we need to use B_TREE with partial match; wait for next update...

DB_File, using the DB_BTREE format is handy for that. Since keys are stored in lexical order, it suffices to iterate over the keys, and delete the previous key if it matches the current one:

use strict; use warnings; use DB_File; use Fcntl; use File::Temp qw(:POSIX); use Data::Dumper; $Data::Dumper::Indent = 1; my $filename = tmpnam(); # temporary file my %h; # tied DB_File hash tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0666, $DB_BTREE or die "Cannot open $filename: $!\n"; %h = ( 'rendition' => '3', 'automation' => '2', 'saturation' => '3', 'mass creation' => 2, 'automation technology' => 2, 'automation technology process' => 3, ); my $prev; for (keys %h) { delete $h{$prev} if $prev && /^$prev/; $prev = $_; } print Dumper(\%h); # cleanup untie %h; unlink $filename; __END__ $VAR1 = { 'automation technology process' => '3', 'mass creation' => '2', 'rendition' => '3', 'saturation' => '3' };

DB_File uses a file on disk, so quite long hashes are possible. Since keys used in a for loop just iterates over the keys, there's no need to retrieve all the keys and sort them. BTREE has them sorted already.

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

In reply to Re: retain longest multi words units from hash by shmem
in thread retain longest multi words units from hash 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.