Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Reading from a file

by castaway (Parson)
on Aug 26, 2003 at 10:01 UTC ( [id://286642]=note: print w/replies, xml ) Need Help??


in reply to Reading from a file

There are a couple of possible mistakes:

1. You don't check if the open() worked, and you're not passing it a full pathname to the file, are you sure it's found the file? Try using:

open(file1, "sample.txt") || die "Can't open sample.txt ($!)";
- the code will end (die) if the file cant be found or opened, including the '$!' will give you the system error message, such as 'cant find file' etc.

2. You're using file1, whether the open actually worked or not. To get Perl to tell you if the file handle is actually readable, try putting 'use warnings;' at the top of your code.

Do you realise that you are trying to print the first line of the file, then assign the second line to $line1, and then print that? Each line is read only once from the file handle, the next access will get the following line..

C.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://286642]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2024-04-24 22:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found