Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

File Open Error

by stallion (Acolyte)
on May 09, 2012 at 13:13 UTC ( [id://969617]=perlquestion: print w/replies, xml ) Need Help??

stallion has asked for the wisdom of the Perl Monks concerning the following question:

Im currently learning perl and Im trying to open many text files and read the contents and extract a string from the files and the corresponding file name ...Th snippet im using is

@scriptfiles=glob('*.txt'); foreach my $file (@checkfiles) { open($file, "data.txt") or die("Unable to open file"); # read file into an array @data = <$file>; # close file close($file); foreach my $line(@data) { #print $line; if ($line =~ m/(Apple|Orange|Litchi)/) { $Sheet->Cells($row,$col-1)->{'Value'} = $file; $Sheet-> Cells($row,$col+6)->{'Value'} = $line ; $row=$row+1; }

I cant able to open the file and read the contents..pls rectify my mistake..

Replies are listed 'Best First'.
Re: File Open Error
by aaron_baugher (Curate) on May 09, 2012 at 13:41 UTC

    Your open command says to open the file named "data.txt," and put a file descriptor attached to it in the scalar variable $file. Probably not what you intended.

    By the way, while there are times that it makes sense to slurp an entire file into an array and then step through the array, it's not a good habit to get into when you don't need to.

    # Instead of: open $file, .... my @data = <$file>; close $file; foreach my $line (@data){ # do stuff with $line } # get in the habit of doing this: open $file, .... while( my $line = <$file> ){ # do stuff with $line } close $file;

    Aaron B.
    Available for small or large Perl jobs; see my home node.

Re: File Open Error
by cdarke (Prior) on May 09, 2012 at 13:49 UTC
    Did you notice there were a few syntax errors?
    You probably meant something like this:
    foreach my $file (glob('*.txt')) { open(my $fh, $file) or die("Unable to open '$file': $!"); while (my $line = <$fh>) { if ($line =~ m/(Apple|Orange|Litchi)/) { $Sheet->Cells($row,$col-1)->{'Value'} = $file; $Sheet->Cells($row,$col+6)->{'Value'} = $line ; $row=$row+1; } } # close file close($fh); }
Re: File Open Error
by Anonymous Monk on May 09, 2012 at 13:23 UTC

    I cant able to open the file and read the contents..pls rectify my mistake..

    Remember this question of yours? Extract Multiple Tags

    When you search the code you posted there for "or die" what do you notice? How is that "or die" different from the one you have posted here?

    What do you find when you search perlintro for "or die"? How is that "or die" different from the one you have posted here?

Re: File Open Error
by BillKSmith (Monsignor) on May 09, 2012 at 14:08 UTC

    always use use strict; use warnings;

    What do they tell you about @checkfiles?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://969617]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-25 05:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found