in reply to A unique question help!

I'm unclear why you cannot use UNIQUE DISTINCT in your SQL query, but you can use a hash to check if you've seen a given data value already. You've not made it clear what you expect to be unique, but if you assume that "user" should only be displayed once, you might want something like:

$user_data = " <table cellpadding=\"0\" cellspacing=\"0\"> <thead> <tr> <td colspan=\"4\">User List</td> </tr> </thead>"; my %seen; for (my $i = 0; $i < @$sql_data; $i++) { my $user = $sql_data->[$i]{user}; if ($seen{$user}) { # <-- added flow control next; } $seen{$user} = 1; my $link = "<a href=\"goto.pl?user=$user\">$user</a>"; my $first = $sql_data->[$i]{first}; my $last = $sql_data->[$i]{last}; my $phone = $sql_data->[$i]{phone}; my $mail = $sql_data->[$i]{mail}; $user_data = "$user_data<tr><td><div>$show</div></td><td><div>$las +t, $first</div></td><td>$phone</td><td><div>$email</div></td></tr>"; } $user_data = "$user_data</table>";

If your condition is more complex, you can use a list of lists (perllol) to check uniqueness in a parallel fashion.

Update: Changed UNIQUE to DISTINCT above. Not quite sure why I always forget that keyword. Thanks, roboticus.

Replies are listed 'Best First'.
Re^2: A unique question help!
by Anonymous Monk on Oct 14, 2009 at 17:34 UTC
    I have to correct this line:
    $user_data = "$user_data<tr><td><div>$show</div></td><td><div>$last, $ +first</div></td><td>$phone</td><td><div>$email</div></td></tr>";

    with
    $user_data = "$user_data<tr><td><div>$link</div></td><td><div>$last, $ +first</div></td><td>$phone</td><td><div>$email</div></td></tr>";

    I would like to test all the data in "$user_data" to be unique.
Re^2: A unique question help!
by Anonymous Monk on Oct 14, 2009 at 17:45 UTC
    Interesting, can you explain this line:
    $seen{$user} = 1;

    Thanks!
      %seen is a hash, and so $seen{$user} = 1; sets the key stored in $user in %seen to 1 (perldata). 1 is true in Perl, so the second time the same key is encountered, the test if ($seen{$user}) { will be true, and the code will execute next, skipping ahead in the for loop.
Re^2: A unique question help!
by Anonymous Monk on Oct 14, 2009 at 17:41 UTC
    It worked, thanks a lot!