my $flag = 0; for (my $i = 0; $i < @$sql; $i++) { ... $unique{$user} = 1; $flag++; # increase the variable ... ... if ($flag % 2) ... ... }