Improve company productivity with a Business Account.Sign Up

x
?
Solved

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

Posted on 2000-04-14
6
Medium Priority
?
215 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
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
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 800 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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 …
Article by: Tammy
MySQLTuner is a script written in Perl that allows you to review a MySQL installation quickly and make adjustments to increase performance and stability. The current configuration variables and status data is retrieved and presented in a brief forma…
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…
Six Sigma Control Plans

595 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