Dear Perlmonks,

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:

sub hashValueDescendingNum { # sort hash by key length in descending order length($a) <=> length($b) }
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'.

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

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.