Solved

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

Posted on 2000-04-14
6
147 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
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
perl match and sort unique result 2 122
Perl efficient DB Call 8 75
PHP equivalent of opening a com object 5 64
create a gui in perl 3 46
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now