in reply to finding the first unique element in an array

This is a FAQ. See perldoc -q duplicate. The usual solution is
my %seen; my @uniq=grep !$seen{$_}++, @name1;

Replies are listed 'Best First'.
Re^2: finding the first unique element in an array
by Limbic~Region (Chancellor) on Jul 19, 2005 at 14:27 UTC
    blazar,
    If I am understanding the question correctly, this is not a FAQ and your solution has nothing to do with the question being asked.

    The object isn't to remove duplicates but to find the first entry that doesn't have a duplicate and then print the corresponding element (via the index) in another array.

    my (@name1, @name2, @percent); # initialized elsewhere my %seen; ++$seen{$_} for @name1; for ( 0 .. $#name1 ) { next if $seen{ $name[$_] } > 1; print $name2[$_]; last; }
    It is completely possible that I am the one who has interpreted the problem wrong though.

    Cheers - L~R

    Update: Oversight corrected thanks to blazar below. That's what you get for not testing your code ;-)

      I think we're both (partly) wrong. Actually the subject seems to support your interpretation. OTOH I'm convinced that part of the code supports mine. Actually I didn't notice the idx thing and on a better reading I think that what he wants may be along the lines of
      my %seen; for (0..$#name1) { next if $seen{ $name1[$_] }++; print $name2[$_]; }
      But unless I'm mistaking something obvious your code won't work as you're populating %seen with
      ( 0 => 1, ..., $#name1 => 1 )
      .