Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Perl not reading all lines of file properly

Posted on 1997-06-20
5
Medium Priority
?
812 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 100 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

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 …
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…
Six Sigma Control Plans

715 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