http://www.perlmonks.org?node_id=1188019


in reply to Trudging along the learning perl path.

welcome AnonymousMonk,

First of all it is a good thing you want to learn experiencing but, BUT! trying to do something without using the simpler, acclaimed way (an hash for uniqueness) maybe is not so good idea, worst if you are beginning. The satandard way is a FAQ infact (infaq?)

Anyway it is possible for example using the smart match operator (it is sperimental) which syntax is ~~ see Smartmatch-Operator and act, in the form $scalar ~~ @array like if array contains scalar

my @arr = (0,1,2,3,1,2); my @uniq; foreach (sort{$a<=>$b} @arr){ unless($_ ~~ @uniq){push @uniq, $_} } print join ' ', @uniq;

This is very similar to another existing builtin tool any from List::Util (update: fixed from Utils to Util) core module:

use List::Util qw(any); my @arr = (0,1,2,3,1,2); my @uniq; foreach my $it (sort{$a<=>$b} @arr){ unless(any{$_ == $it}@uniq){push @uniq, $it} } print join ' ', @uniq;

I say this because it is very important and crucial to know which Perl tool to use for a task: this can save hours of your precious time: get the habit to read across perldoc to know everyday more: functions tutorials, core modules.. perldoc save your day!

Then, instead of start spending several hours trying to get uniqness without using an hash it will be by far better to read some good manual like ModernPerl.pdf the free book, or to get a copy if the even old PerlCookbook (a must have to get a wide panorama of solutions and fields to play with).

I say this because me too i'm somehow a Perl-just-for-fun (I use at work too but when conceiled and i just show perl result and benefits when i've done).

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.