Perl Quiz
I've been interviewing people, some of whom have Perl skills. Here's a question I like to ask:
Write a short perl function to find the unique elements of an array.
And here are some possible answers:
- sub uniq { return keys %{{ map{$_=>1} @_}}; }
- sub uniq { my %seen; return grep { !$seen{$_}++ } @_; }
- sub uniq { my %uniq; @uniq{@_} = (); return keys %uniq; }
A one-line comment explaining what each one does is left as an
exercise to the reader.
Labels: perl