I would like to delete hash keys that are a substring of other hash keys.
My data looks like this:
this is a test => 2 is a test => 2 a test => 1
I get this data from an array in which I count the unique elements and put them in a hash, counting the number of occurrences:
sub count_unique { # extract unique elements of array and count occurrences my @array = @_; map { $count{$_}++ } @array; map { "$_ = ${count{$_}}\n"} sort keys(%count); return %count; }
I then sort the hash by the string length of the key, decreasingly:
Where I'm stuck is when I try to find out if element+1 is a substring of the current element. In the example below, a loop should remove everything except 'this is a test'.sub hashValueDescendingNum { # sort hash by key length in descending order length($a) <=> length($b) }
this is a test => 2 is a test => 2 a test => 1
I though of getting the hash's size and then doing a for $i loop inside it, but that's as far as I got.
Thanks in advance for your help.
Larry
In reply to hash substrings by perlcat
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |