Solved

Perl not reading all lines of file properly

Posted on 1997-06-20
5
780 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl for loop for 2000 ms 7 100
how to exit a  for loop inside a function with return value in bash 5 99
Perl script to delete older files 6 86
html form to write data to csv 24 114
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

770 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