in reply to retain longest multi words units from hash

This is what I managed to do after reading the various answers. I consider a multi words unit (MWU) to be contained in a bigger one only if the sequence of words is the same (so technology process is contained in automation technology process', but 'automation process is not). I do not have other constraints I can think of.

Be aware, my code is very verbose....

use strict; use warnings; use Data::Dump qw/pp dd/; my %output; my %phrases = ( 'rendition' => '3', 'automation' => '2', 'saturation' => '3', 'mass creation' => 2, 'automation technology' => 2, 'automation technology process' => 3, 'technology process' => 5, 'automation process' => 2, ); foreach my $keyPhrases (sort hashKeyLength (keys(%phrases))) { my $TermToBeRetained=1; print "\nWorking on * $keyPhrases *\n"; foreach my $keyOutput (keys %output){ print "\tChecking if * $keyPhrases * is contained in * $keyOut +put *: "; if ($keyOutput =~/$keyPhrases/){ print "Yes\n"; $TermToBeRetained=0; last; } else{ print "No!\n"; $TermToBeRetained=1; } } if ($TermToBeRetained eq 1){ print "Retaining $keyPhrases\n"; $output{$keyPhrases} = $phrases{$keyPhrases};; } else{ print "Skipping $keyPhrases\n"; } } print "MY RESULT\n"; print pp \%output; sub hashKeyLength { count_units_in_phrase($b) <=> count_units_in_phrase($a) || $b cmp +$a } sub count_units_in_phrase { my $string=shift; my $num; $num++ while $string =~ /\S+/g; return $num }

What do you think of it?

Discipuls added code tags 28 July 2018

Replies are listed 'Best First'.
Re^2: retain longest multi words units from hash
by AnomalousMonk (Archbishop) on Jul 29, 2018 at 03:47 UTC
Re^2: retain longest multi words units from hash
by Anonymous Monk on Jul 28, 2018 at 15:22 UTC

    Put code tags around it.

      Unfortunately I can not edit my post

        Easy solution: log in in the future. :)

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice