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_
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |