Best way to prevent duplicate element from being output in real time during loop

I see a lot of related "related" questions, but none of them answered this particular scenario.

During the while / for loop that parses the result set generated from the SQL select statement, what is the best way to prevent the next line from being output if the line before it contains the same field data (be it the first field or x-field)?

For example, if two lines:

('EML-E','jsmith@mail.com','John','Smith')
('EML-E','jsmith2@mail.com','John','Smith')

      

What is the best way to print only the first line based on the EML-E being the same on both lines?

I am doing this now:

  • Storing the first field (defined for my scenario) in a 2 element array (dupecatch [1])
  • Check if dupecatch [0] = dupcatch [1] (duplicate - escape loop using 's')
  • After processing the line, set dupecatch [0] = dupecatch [1]

    while ($DBS->SQLFetch() == *PLibdata::RET_OK)
    {
        $s=0; #s = 1 to escape out of inside loop
        while ($i != $array_len and $s==0)
        {
            $rowfetch = $DBS->{Row}->GetCharValue($array_col[$i]);
            if($i==0){$dupecatch[1] = $rowfetch;} #dupecatch prevents duplicate primary key field entries
            if($dupecatch[0] ne $dupecatch[1])
            {
                dosomething($rowfetch);
            }
            else{$s++;}
            $i++;
        }
        $i=0;
        $dupecatch[0]=$dupecatch[1];
    }
    
          

0


source to share


2 answers


This is the standard way if you only care about duplicate items in the string, but $ dupecatch [0] is usually called $ old and $ dupecatch [1] is usually just the variable in question. You might say that an array is inappropriate because you are only referring to its indices.

If you want to avoid all duplicates, you can use the% seen hashing:



my %seen;
while (defined (my $row = get_data())) {
    next if $seen{$row->[0]}++; #skip all but the first instance of the key
    do_stuff();
}

      

+7


source


I suggest using DISTINCT in your SQL statement. This is probably the easiest solution.



+1


source







All Articles