in reply to hash substrings

I would rather sort the keys in ascending order (of key length), store them in an array, and compare the elements of that array, something like:

use strict; use warnings; my %h = ( 'this is a test' => 2, 'is a test' => 2, 'a test' => 1 ); my @keys = sort {length $a <=> length $b} keys %h; for ( 0 .. $#keys-1){ if ($keys[$_+1] =~ /$keys[$_]/){ delete $h{$keys[$_]}; } }

Hope this helps

citromatik

Replies are listed 'Best First'.
Re^2: hash substrings
by perlcat (Novice) on Jan 27, 2009 at 09:03 UTC
    thanks, it worked like a charm!

    Larry

      Why is it enough for you to compare the strings only to the next element? Are keys like

      "This is a test" "something" "a test"

      impossible in your setting?