Solved

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

Posted on 2000-04-14
6
183 Views
Last Modified: 2011-04-14
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
Comment
Question by:Biffo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 1

Expert Comment

by:olthoff
ID: 2715765
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
 
LVL 2

Author Comment

by:Biffo
ID: 2715860
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
 
LVL 16

Expert Comment

by:maneshr
ID: 2716046
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:jyu_88
ID: 2716138
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
 

Accepted Solution

by:
sheld24 earned 200 total points
ID: 2716302
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
 
LVL 2

Author Comment

by:Biffo
ID: 2716701
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question