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".

    Add a comment to this post:

    Your name:

    Your email: Email me new comments on this post
      (Your email will not be published on this site, and will only be used to contact you directly with a reply to your comment if needed. Oh, and we'll use it to send you new comments on this post it you selected that checkbox.)


    Your website:

    Comments: