
Laurent_R
Veteran
/ Moderator
Sep 16, 2012, 9:36 AM
Post #2 of 10
(5365 views)
|
Re: [GAR] Find last-backup file within a directory using Perl
[In reply to]
|
Can't Post
|
|
The easiest way to remove duplicates is genberally to use a hash to store the items that you have already seen. When you see a server name, check if it is in the hash. If it already there, juste ignore it. If it is not there, then add it. At the end, your hash will contain server names without duplicate. For example you could change your code as follows:
my %servernames; # ... foreach my $file (@files) { $file = substr($file,0,-9); $servernames{$file} = 1 unless exists $servernames{$file}; } # the %servernames hash now contains on sample of every filename Actually, the "unless exists..." part is not even mandatory, you'll have still only one sample of the server names it you don't put it, but I find the intent is clearer if you put it.
|