简体   繁体   中英

Convert an excel file to txt and open in perl

I have an excel file with my data. I saved it as a tab delimited txt file.

But if I do a simple perl script:

open(IN, '<', 'myfile.txt') or die;
while (defined(my $line = <IN>)){
    print "$line\n";
}
close IN;

it only prints out one line, but it contains all the data - just in one line If I use another data file, there are no problems, so i think there is a problem convertin the excel file to a txt file.

can anybody help me?

try while (<IN>) instead. Your condition beats the while magic..

I'd change the loop to:

while(my $line = <IN>) { ... }

There's no need to use defined().

I am not sure if have this answered yet. But, first make sure you have the following in your code:

use strict;
use warnings;

This will give you debugging help that you would receive otherwise. Using the above will give you more messages that can help.

When I put your open command in a current program I am working on I received this debugging message:

Name "main::IN" used only once: possible typo at ./test.pl line 37

You also may want to use a file handle so Perl can remember where go. This is the "new" way to open files in Perl and is explained on the online perldoc. Just search for "perl file handle open." I learned to do my open's this way:

open my $in '<', 'myfile.txt' or die;

Then, you can just run the following:

while ( my $line = <$in> ) { ... }

There is a better way to do this if you ever have been introduced to Perl's default variable, yet I don't think that you have so the above solution may be the best.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM