Solved

Perl not reading all lines of file properly

Posted on 1997-06-20
5
762 Views
Last Modified: 2012-06-27
DOH!!!  Stupid mistake.  I fixed it.  Had a variable double counting occasionally that just so happened to print nearly all of my old stuff and very little of the new.  (KICK!) :-)

I have a perl script that reads in data from a flatfile relational database and prints the appropriate info for the user.  It is getting hard to maintain, so I imported it into a Microsoft Excel document using the delimiters I had set up.  I can modify it much easier now.  I saved the file as a .csv comma delimitered fields.  Now when I read the file with my program, all fields and rows I added/modified while in Excel do not appear.  I don't know why.  Any help would be greatly appreciated.  I use a command like this to read the file.

open (In_File,"mxdata.csv");
while (<In_File>)
{
     ($num,$name,$r1,$r2,$r3) = split(/,/,$_,5);
     if ("$name" eq $input{'name'})
     {
          $numarray{$pointer} = $num;
          $namarray{$pointer} = $name;
          etc..........
          There are multiple names.
      }
      $pointer++
}

Thanks for the help.
0
Comment
Question by:Madness
5 Comments
 

Author Comment

by:Madness
ID: 1204331
Edited text of question
0
 

Author Comment

by:Madness
ID: 1204332
Edited text of question
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1204333
May be you simply have a (disk) syncing problem.

Are these, Excel and your perl script, two seperate processes?
If so and they run simultaniouslly, your perl script just reads
from the file what it contains when the open() call is executed.
Rewriteing (Excel) the file while reading (perl) may return
unexpected results.
0
 

Accepted Solution

by:
bron_a earned 50 total points
ID: 1204334
I suggest to re-write the program as following:

open (infile,"mxdata.csv");
while (!eof (infile)) {
  $line = <infile>;
  @parts = split (/\,/, $line);
   $num = $parts [0];
   $name = $parts [1];
   $r1 = $parts [2];
   $r2 = $parts [3];
   # etc....
 }
close (infile);

This will guarrant that all the file lines are being read with no dependence of the environment.
You should also check, if there is a newline character at the last line of the input file.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1204335
Some comments:
Always check the return value from open

      open(In_File,"mxdata.csv") || warn "Can't open xdata.csv  $!";

Using consecutive $pointer++ hashes suggest using an array instead

      push(@namarray,$name);
      #etc...

Quoting "$name" is not neccesary

      if( $name eq $input{'name'} ){ ... }

Other than ommiting that test, and truncating $r3 if it contains a comma,
(which doesn't need to be escaped in the split)
I don't see much point to bron_a's suggested re-write.
0

Featured Post

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.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Sending email via Perl on Windows 3 125
perl to mysql 5 128
delete query using perl dbi 3 92
Add additional column to .csv using Perl. 8 121
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…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 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

11 Experts available now in Live!

Get 1:1 Help Now