| Unique elements in a perl array |
I want to have an array in perl with only unique elements. My current hacky way is to misuse a hash, is there a nicer way of doing this?
# This function is similar to the above, but returns a list of the
# directories containing at least one image.
#
# Pass in the path to the parent directory
sub getdirectories{
my($path) = @_;
my(%directories);
find(sub{
# Again, this needs to be tweaked if other image formats are
# to be supported
if($File::Find::name =~ /\/([^\/]*\.jpg)$/i){
# This is a horrible, horrible hack
$directories{$File::Find::dir} = "yes";
}
}, $path);
return keys %directories;
}
[tags: unique array item]
posted at: 22:04 | path: /perl | permanent link to this entry
-
#2
Hasan
This is covered in perlfaq4 (see, "How do I remove duplicate elements from a list or array?" What you want is solution "b" or "d".
