in reply to Braino - why is this not working?

You are using $_ from @res instead of using $_ from outer for loop in grep statement.
Instead try this
perl -le '@arr=qw(a b a c); my @res; foreach(@arr) { $__=$_;$ans= grep /^$__$/,@res; print "\$_: $_ ans: $ans";push @res,$_ unless $ans;} print "@res"' $_: a ans: 0 $_: b ans: 0 $_: a ans: 1 $_: c ans: 0 a b c

Replies are listed 'Best First'.
Re^2: Braino - why is this not working?
by perl-diddler (Chaplain) on Oct 19, 2010 at 04:22 UTC
    Yup -- the grep's use of $_ was over-writing my 'for' loops usage.

    Thanks much for...helping me get this problem out of my head (like a song that gets stuck....). I could work around, it, but till bugged me if ya know what I mean, as it had to be something obvious staring me in my face that I couldn't see...

    Hate it when that happens...