If you have an array with list elements and need them all to be unique, you can rename the duplicate list elements with this code.
#!/usr/bin/perl -w use strict; my @list = qw{ key value marker key }; my %seen = (); my @newlist = (); foreach my $item (@list) { if (!$seen{$item}) { $seen{$item} = 1; push(@newlist, $item); } else { push(@newlist, "$item" . "$seen{$item}"); $seen{$item}++; } } print @newlist;

Replies are listed 'Best First'.
Re: Rename Duplicate List Elements in an Array
by ambrus (Abbot) on Oct 05, 2005 at 07:34 UTC

    This problem is not that simple to solve. If you take

    my @list = qw{ key value marker key key1 };
    then the new list will have key1 twice.
Re: Rename Duplicate List Elements in an Array
by parv (Parson) on Oct 05, 2005 at 10:27 UTC

    As ambrus noted, your solution, of appending some extra text, is at the mercy of characters at the end making the data. One solution, then, would be to scan the data to select the suffix that already does not exist in data.

    Thinking out loud ... or, perhaps use (a heap data structure along w/ a count of each element which would then translate to) a priority list. That does not exactly make each element to be unique as presented in OP, but allows to define uniqueness in other way. That seems, however, quite heavy when we already can use %seen (populated inside the loop) instead of throwing it away. Hmm ...

    Addendum, Oct 6,2005: I was thinking of priority value as a count of each string. If there are only few multiplcates, a priority list will be pointless. Using a heap will give you elements in sorted way (meaning lots of comparisons) even if sorting is not a requirement. So i would just use %seen under a different name and w/o the @newlist. Further, count can be stored along w/ list index of each element in a n array reference to encfore input order.

Re: Rename Duplicate List Elements in an Array
by awohld (Hermit) on Sep 23, 2015 at 18:14 UTC
    I happened to google this problem almost exactly 10 years later only to find this like a message in a bottle to myself in the future! :)

    I'd like to make it look a bit nicer:
    my @list = qw( one two three one ); @list = make_unique( @list ); sub make_unique { my @list = @_; my %seen; my @new_list; foreach my $item ( @list ) { if ( not defined $seen{$item} ) { $seen{$item} = 1; push @new_list, $item; } else { push @new_list, $item . '_' . $seen{$item}; $seen{$item}++; } } return @new_list; }
      #!/usr/bin/perl use strict; use warnings; my @list = qw( one two three one one two_2 two_2 two two_2 two two_3 t +wo one ); @list = make_unique( @list ); print "@list\n"; sub make_unique { my (%seen, $try); map { my $n = $seen{$_}; $n++ while $seen{$try = $n ? $_ . "_$n" : $_}++; $try } @_; }