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

I am trying to do here is after processing data from the DB in this "for loop", display the information to the screen, but I am running into a problem with duplicated values, I just can’t figure it out how to prevent this, I am trying to explain the issue with code, I hope some of you can understand what I am trying to do, otherwise I can answer more questions to explain my problem here, thanks!

for (my $i = 0; $i < @$sqlinfo; $i++) { my $user = $sqlinfo->[$i]{user}; # @check_user previously initiated - check here against previously f +ound names to avoid showing duplicated to the screen next if "@check_user" =~ /(^|\s)$user(\s|$)/; my $number = sqlinfo->[$i]{number}; my $show = "<a href=\"call.pl?user=$user\">$user</a>"; my $first = sqlinfo->[$i]{first}; my $lastn = sqlinfo->[$i]{lastn}; my $tel = sqlinfo->[$i]{tel}; my $email = sqlinfo->[$i]{email}; # @number_check has been previously initiated before if ("@number_check" =~ /(^|\s)$number(\s|$)/ ) { # The $user values here are: Joe1 Mary1 Joe1 Mary1 from $user variab +le as an example, how to avoid showing duplicated values in $userfina +l # I am trying to show Joe1 and Mary1 I tried the code below but its +not working. #trying my %unique; if ($unique{$user}) { next; } $unique{$user} = 1; #end trying - if I use the above nothing shows. #here its still printing all the names ignoring unique names. print "<br>Print this to check if I only got unique user: *$user*<br> +"; # @userfinal has been previously initiated before $userfinal = "$userfinal<tr><td>$show</td><td>$lastn, $firs</td><td>$ +tel</td><td>$email</td></tr>"; } }


Thanks again!

Replies are listed 'Best First'.
Re: Duplicated values in a For Loop
by ikegami (Patriarch) on Jan 19, 2010 at 19:17 UTC
    use HTML::Entities qw( encode_entities ); use URI::Escape qw( uri_escape ); my %seen_users; my %seen_numbers; for my $rec (@$sqlinfo){ next if $seen_users{ $rec->{user} }++ || $seen_numbers{ $rec->{number} }++; $userfinal .= sprintf( '<tr>' . '<td><a href="call.pl?user=%s">%s</a></td>' . '<td>%s</td>' . '<td>%s</td>' . '<td>%s</td>' . '</tr>', uri_escape($rec->{user}), encode_entities($rec->{user}), encode_entities("$rec->{lastn}, $rec->{first}"), encode_entities($rec->{tel}), encode_entities($rec->{email}), ); }
      The question here would be how to find the unique value in $user.
        I cannot process your sentence into something that makes sense.
Re: Duplicated values in a For Loop
by keszler (Priest) on Jan 19, 2010 at 20:27 UTC
    It looks to me like $sqlinfo is the result of a database query, probably of the $sth->selectall_arrayref variety, and that those results contain multiple rows for some users. Would it not be simpler to modify the SQL query to obtain one row per user?

    I'm really reaching here, but assuming a well-normalized database the SQL query probably joins three tables, something like:

    select p.number, p.user, p.first, p.lastn, t.tel, e.email from people p left join telephone t on t.number = p.number left join email e on e.number = p.number
    If this is the case, you're getting multiple rows for users with multiple telephone numbers and/or email addresses.

    Changing the SQL query to get distinct users would require some way to select only one each tel and email.

    If I'm guessing correctly, and changing the query is feasable, I'd need the database type (Oracle, MSSQL, MySQL, etc.) and the table schemas to work out a suitable WHERE clause.