• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 212
  • Last Modified:

Why em I getting duplicate data in this simple loop...

Here is my problem with the below code:

I loop through a directory with a dozen text files and match for specific strings in each one as shown below. Works fine for my purposes except one tiny little annoyance: each following $key# string seems to hold a copy of the previous $key# string found in the previous file. So if File #1 KEY1 = california and File #2 KEY1 = NY then File #2 matches would be printed as:

Californa
NY

The above should only show NY for File #2 and not Californa and NY since California was only found in File #1 and not #2.

Now, $email and $client are returned properly for each file searched in the loop, it's just the $key#'s seem to keep showing previous matches as well as new ones :(

Maybe someone can tell me where my logic fails me here and how I should properly code for this!


---------snipet---------
   print "Looping through Customer Accounts....\n";

    $customer = "$dir/customer/";
    opendir (DIR, $customer);  
    @datfiles = readdir(DIR);  
    closedir (DIR);    

      foreach $dfile (@datfiles) {
      $datpath = $customer . $dfile;
            open (FILE, $datpath);
            @datfile = <FILE>;
            close (FILE);
            foreach $datline (@datfile) {

$email = $1 if ($datline =~ m@^EMAIL.*?(\w.*)@i);
$client = $1 if ($datline =~ m@^CLIENT.*?(\w.*)@i);
$key1 = $1 if ($datline =~ m@^KEY1.*?(\w.*)@i);
$key2 = $1 if ($datline =~ m@^KEY2.*?(\w.*)@i);
$key3 = $1 if ($datline =~ m@^KEY3.*?(\w.*)@i);
$key4 = $1 if ($datline =~ m@^KEY4.*?(\w.*)@i);
$key5 = $1 if ($datline =~ m@^KEY5.*?(\w.*)@i);
$key6 = $1 if ($datline =~ m@^KEY6.*?(\w.*)@i);
$key7 = $1 if ($datline =~ m@^KEY7.*?(\w.*)@i);
$key8 = $1 if ($datline =~ m@^KEY8.*?(\w.*)@i);
$key9 = $1 if ($datline =~ m@^KEY9.*?(\w.*)@i);
$key10 = $1 if ($datline =~ m@^KEY10.*?(\w.*)@i);
} # dat lines

print "$client terms are:\n";
print "$key1\n" if (defined($key1));
print "$key2\n" if (defined($key2));
print "$key3\n" if (defined($key3));
print "$key4\n" if (defined($key4));
}
0
Biffo
Asked:
Biffo
1 Solution
 
olthoffCommented:
Are you sure that it is the same keyvalue that is being printed?  NY and California = $key1? and not this:
$key1 is set to NY in file 1
$key2 is set to California in file 2.
When you print they will have still have the values because you never reset the key#'s.
0
 
BiffoAuthor Commented:
olthoff,

-----------
Are you sure that it is the same keyvalue that is being printed?  NY and California = $key1?
-------------
Yup I am sure, Californaia = $key1 only in File #1.
 
--------------
When you print they will have still have the values because you never reset the key#'s.
-----------------

Well I realize that, but When I look at $client and $email they are always printed properly for the individule file they were found in and they are not being reset, so that confusses me, if it was a reset thinggy then why don't I see $email and $client exhibiting same behavior as $keys?


0
 
maneshrCommented:
u would suggest you undef your key variables before re starting the loop again as follows..

print "$key4\n" if (defined($key4));
undef $key1;
undef $key2;
undef $key3;
undef $key4;


0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
jyu_88Commented:
exactly as olthoff pointed out, some of the keys are not set in the next run since it is not found, so they get printed for the previous value. So, if you print "key1=<$key1>\n", you shall see it is not key1 print out two values, it is key1 from previous file and key2 from current file.

since you probably have mail/client in all files, they have new values assigned to them (reset) everytime, so, they have fresh values everytime.

It'd be a good idea to undef variables explicitly when it goes out of its necessary scope.
0
 
sheld24Commented:
Here is your answer:




print "Looping through Customer Accounts....\n";

                    $customer = "$dir/customer/";
                    opendir (DIR, $customer);  
                    @datfiles = readdir(DIR);  
                    closedir (DIR);    

                      foreach $dfile (@datfiles) {
                      $datpath = $customer . $dfile;
                            open (FILE, $datpath);
                            @datfile = <FILE>;
                            close (FILE);
                            foreach $datline (@datfile) {

                $email = $1 if ($datline =~ m@^EMAIL.*?(\w.*)@i);
                $client = $1 if ($datline =~ m@^CLIENT.*?(\w.*)@i);
                $key1 = $1 if ($datline =~ m@^KEY1.*?(\w.*)@i);
                $key2 = $1 if ($datline =~ m@^KEY2.*?(\w.*)@i);
                $key3 = $1 if ($datline =~ m@^KEY3.*?(\w.*)@i);
                $key4 = $1 if ($datline =~ m@^KEY4.*?(\w.*)@i);
                $key5 = $1 if ($datline =~ m@^KEY5.*?(\w.*)@i);
                $key6 = $1 if ($datline =~ m@^KEY6.*?(\w.*)@i);
                $key7 = $1 if ($datline =~ m@^KEY7.*?(\w.*)@i);
                $key8 = $1 if ($datline =~ m@^KEY8.*?(\w.*)@i);
                $key9 = $1 if ($datline =~ m@^KEY9.*?(\w.*)@i);
                $key10 = $1 if ($datline =~ m@^KEY10.*?(\w.*)@i);
                } # dat lines

                print "$client terms are:\n";
                print "$key1\n" if ($key1 ne '');
                print "$key2\n" if ($key2 ne '');
                print "$key3\n" if ($key3 ne '');
                print "$key4\n" if ($key4 ne '');
           

                 # Clear strings

                  $key1 = '';
                  $key2 = '';
                  $key3 = '';
                  $key4 = '';
                  $key5 = '';
                  $key6 = '';
                  $key7 = '';
                  $key8 = '';
                  $key9 = '';
                  $key10 = '';

                   } # for ea file  
0
 
BiffoAuthor Commented:
OK, now I understand why my $keys were acting way they were and not $email and $client: Because $keys were not always being found like $client and $email !!

Thanks a bunch guys for the insight!

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now