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

Hi Monks!
I am getting data from this table, but my issue is that some duplicated information has been displayed, I can not prevent that on the sql query side of the code, I need to do it on the Perl side, here is the part of the code doing it,
$user_data = " <table cellpadding=\"0\" cellspacing=\"0\"> <thead> <tr> <td colspan=\"4\">User List</td> </tr> </thead>"; for (my $i = 0; $i < @$sql_data; $i++) { my $user = $sql_data->[$i]{user}; 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>$last, $ +first</div></td><td>$phone</td><td><div>$email</div></td></tr>"; } $user_data = "$user_data</table>";

Right there a have to test $user_data for duplicated values, and only get the unique ones.
Any help?!
Thank you!

Replies are listed 'Best First'.
Re: A unique question help!
by kennethk (Abbot) on Oct 14, 2009 at 17:19 UTC
    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.

      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.
      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.
      It worked, thanks a lot!
Re: A unique question help!
by mickep76 (Beadle) on Oct 15, 2009 at 11:37 UTC

    Hi

    Just assign it to a hash and the hash will automagically remove all duplicates.

    Example:

    @list = ('a', 'a', 'a', 'b', 'c', 'd'); foreach(@list) { $unique{$_}++ } foreach(sort keys %unique) { printf "$_ : %s\n", $unique{$_} }

    And if you also increase the value for each assignment to the hash you also get the number of duplicates.