There are many ways to do this but one comes to mind that may shorten the
code as well as speed things up just a little (REGEX's are usually very
efficient).  Try this out:

sub update_categories {
my($maincat,$subcat) = @_;
my $process          = 0;
my @scats            = ();
    $subcat =~ s!(.*?)\.cache!$1!g;
    open(CAT,"<$categoryfile") or die $1;
    my @catdata = <CAT>;
    close(CAT);

open(CAT,">$categoryfile") or die $!;
      for my $cdat (@catdata) {
        if(($cdat =~ /^$maincat\:\:/) && ($cdat
=~/(\:\:$subcat\:\:|\:\:$subcat\n$)/)) {
           $cdat =~ s/\n$/$subcat\n/;
        }
        print CAT $cdat;
      } # close for my
    close(CAT);
  return;

}


Hope this helps.

Chris Rogers



-----Original Message-----
From: Mike Blezien [mailto:[EMAIL PROTECTED]
Sent: Thursday, April 03, 2003 11:41 AM
To: Perl List
Subject: Check for duplicate entires


Hello all,

working on creating a category/sub category data file, somthing like this:

Animations::Mini::3D-Miscellaneous
Clip_Art::People-Stick_People::Business::Household-Furniture
Icons::BMP-Computers::32x32icons-Computers

the first field in each line is the main category, followed by their
respective 
sub categories... and this file needs to be updated on a daily basices.. the

problem I'm having is when adding new sub categories to a matching main 
category, is avoiding duplications of sub cateogories.

Example:
Clip_Art::People-Stick_People::Business::Household-Furniture

Clip_Art is the main category, then all others on this line are it's sub 
categories of the Clip_Art main category.

Now when updating the file, if the sub category being added is already in
data 
record is not to add it or update the file. So if a sub category is passed, 
Business(which is already in the data record), what is the best way to check
if 
the sub category exist, and the best approach to updated the file.

this is a snip of the code we're using to add the sub categories to the file
and 
it works fine, but we need to prevent duplications of sub categories for a 
matching main category.

sub update_categories {
my($maincat,$subcat) = @_;
my $process          = 0;
my @scats            = ();
    $subcat =~ s!(.*?)\.cache!$1!g;
    open(CAT,"<$categoryfile") or die $1;
    my @catdata = <CAT>;
    close(CAT);

open(CAT,">$categoryfile") or die $!;
      for my $cdat (@catdata) {
         chomp $cdat;
         @scats = split(/\::/,$cdat);
           if ($scats[0] eq $maincat) {
               push(@scats,$subcat);
           }
         my $newline = join("\::",@scats) . "\n";
        print CAT $newline;
      } # close for my
    close(CAT);
  return;

}

TIA... :)

-- 
Mike<mickalo>Blezien
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Thunder Rain Internet Publishing
Providing Internet Solutions that work!
http://www.thunder-rain.com
Web Hosting
http://www.justlightening.net
Tel:  1(985)902-8484
MSN: [EMAIL PROTECTED]
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to