perlcat has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: hash substrings
by citromatik (Curate) on Jan 27, 2009 at 07:38 UTC | |
by perlcat (Novice) on Jan 27, 2009 at 09:03 UTC | |
by jethro (Monsignor) on Jan 27, 2009 at 15:06 UTC | |
|
Re: hash substrings
by gone2015 (Deacon) on Jan 27, 2009 at 10:08 UTC | |
by jwkrahn (Abbot) on Jan 27, 2009 at 10:43 UTC | |
by gone2015 (Deacon) on Jan 27, 2009 at 10:54 UTC | |
by perlcat (Novice) on Jan 27, 2009 at 18:08 UTC |