Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Non Duplicate 1

Status
Not open for further replies.

DonP

IS-IT--Management
Jul 20, 2000
684
US
I have a sorted array that I want to use, but I don't want duplicates, though I am using the same array in a different part of the script where all must show. How can I do it only in this one place?

[tt]
foreach (@sorted) {
$_->[0] =~ tr/ /+/;
$_->[3] =~ tr/ /+/;
print &quot;<li><a href=\&quot;form.cgi?$data[0],$data[3],8\&quot;><b>$type</b></a>\n&quot;;
}
[/tt]

Don
don@ctagroup.org
Experienced in HTML, Perl, VBScript, PWS, IIS and Apache. Run OS/2 Warp 4, BeOS v5 and Windows NT (only when I have to!)
 
How 'bout saving the last value, and testing the current value to see if it is the same - if it is, then don't use it - go on to the next one. Wouldn't this work?

foreach (@sorted) {
if ($_ eq $saved) {
next;
}

### do your stuff here ###

$saved = $_;
}

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
Thanks! That's exactly what I am trying to do, though I think I've missed something in the actual syntax. It's the $_ part that has me confused as it does not appear that I am comparing anything because I don't know what it is! The $_->[0] is the field out of the array so I tried comparing it to itself, $_->[0] eq $_->[0], but that did not work either. It's working exactly as it was before I started &quot;messing&quot; with it. Here is what I have:

[tt]foreach (@sorted) {
if ($_ eq $_->[0]) {
next;
}
$type = &quot;$_->[0]&quot;;
$type =~ tr/ /+/;
print &quot;<li><a href=\&quot;form.cgi?$type,$type,8\&quot;><b>$_->[0]$s</b></a>\n&quot;;
}[/tt]

Don
don@ctagroup.org
Experienced in HTML, Perl, VBScript, PWS, IIS and Apache. Run OS/2 Warp 4, BeOS v5 and Windows NT (only when I have to!)
 
one question - will duplications always follow each other? if not, post again. what you need to do is have an extra variable to save the value of the last printed line, then compare that value to each line after it, making sure it's not a duplicate. here:[tt]

my $saved;
foreach (@sorted)
{
if ($saved->[0] eq $_->[0])
{
next;
}
$type = &quot;$_->[0]&quot;;
$type =~ tr/ /+/;
print &quot;<li><a href=\&quot;form.cgi?$type,$type,8\&quot;><b>$_->[0]$s</b></a>\n&quot;;
$saved = $_;
}[/tt]

again, this only works in the case of duplicates always following each other. also note that only the first field is compared. if the first field is supposed to vary every time, this will work. but if the first field stays the same while other fields vary, it'll ignore those differences. &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
By the time @sorted gets to this point, this field should be in order (that is, all the same ones together). The table itself is in order now but may not stay that way as items are added or removed, but the array should be in order because it is being sorted earlier on in the script. Seems to work like a charm now too! Thanks a bundle - again!

Don
don@ctagroup.org
Experienced in HTML, Perl, VBScript, PWS, IIS and Apache. Run OS/2 Warp 4, BeOS v5 and Windows NT (only when I have to!)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top