Regular expressions aren't necessary if you sort the hash keys first and take advantage of the hierarchical nature of Win32 pathnames. Sorting insures that the shortest path that has been created or deleted will always show up first and get added first to your final list of changes.

I'm not exactly sure what %current and %prior hold. Assuming that %current stores the results of comparing a past and current directory listing (created directories assigned the value 'created', deleted assigned the value 'deleted'), here is how you would show only the top most directory that has been created.

# always use strict and warnings - debugging for free! use strict; use warnings; my %current = ('C:\1\x\3\4' => 'created', 'C:\1\x\3' => 'created', 'C:\1\x' => 'created' 'C:\1\2\3\4' => 'deleted', 'C:\1\2\3' => 'deleted', 'C:\1\2' => 'deleted' ); my %unique; node: foreach my $node (sort keys %current) { #look for parent directories in %unique #skip this one if we find a match my @aNames = split(/\\/, $node); my $k=''; for my $i (0..$#aNames) { $k .= '\\' if $i; $k .= $aNames[$i]; next node if exists $unique{$k}; } #no match, so this is the actual directory that moved $unique{$k} = $current{$k}; } use Data::Dumper qw(Dumper); print(Dumper(\%unique)); #outputs #$VAR1 = { # 'C:\\1\\x' => 'created' # 'C:\\1\\2' => 'deleted' # };

To understand the above code, you may find it helpful to look up the following links in the Perl documentation:

Best, beth


In reply to Re: Comparing strings to reduce excess results by ELISHEVA
in thread Comparing strings to reduce excess results by grub_

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.