×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Non Duplicate

Non Duplicate

Non Duplicate

(OP)
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?


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

Don don@ctagroup.org http://www.helpscc.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!)

RE: Non Duplicate

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.
http://www.missioncriticallinux.com

RE: Non Duplicate

(OP)
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 "messing" with it. Here is what I have:

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

Don don@ctagroup.org http://www.helpscc.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!)

RE: Non Duplicate

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:

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

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.

"If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."

RE: Non Duplicate

(OP)
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 http://www.helpscc.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!)

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close