Solved

Perl not reading all lines of file properly

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

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

Suggested Solutions

Title # Comments Views Activity
Rename file based on contents in the file? 3 97
crawling gofundme 4 132
add a syntax to a csv file 8 99
Perl Untar File 1 55
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…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…

821 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