Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!

I am running into a problem where I have a "for loop" that is running twice and returning duplicated results. I have no way around this but to stop this "for loop" of running twice somehow. When the "for loop" runs that's what I get, as an example:
user1 user2 user1 user2

I need to have only the first two values since the third and fourth values are duplicated, any way is there a way to know and to stop this "for loop" to run twice by checking if it already ran or by checking the value of the last result, or some other way?
Thank you for the help!

Replies are listed 'Best First'.
Re: Stoping For Loop Help!
by ikegami (Patriarch) on Jan 20, 2010 at 05:18 UTC

    Your request is very unclear. Are you trying to skip over duplicates? If so, you can do

    my %seen_users; for my $user (@users) { next if $seen_users{$user}++; <-- Add this line ... }
    For example,
    my @users = qw( John Jane John Jeff ); my %seen_users; for my $user (@users) { next if $seen_users{$user}++; print "$user\n"; }
    John Jane Jeff
Re: Stoping For Loop Help!
by Anonymous Monk on Jan 20, 2010 at 04:06 UTC
    For loops as a rule, do not run twice, so you need to show your code