Solved

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

Posted on 2000-04-14
6
174 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…

790 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