john.perry.14 has asked for the wisdom of the Perl Monks concerning the following question:

I was wondering if it is possible to read from a text file one line at a time. I am doing a combination perl/python unofficial course at school, and the instructor told us to go to the internet for help. Of course, all the google search/perlmonks results just say to use the whole file, which is not an option. Our current assignment is to create a MadLib, but the file containing the words for the madlib contains 4 MadLibs worth of words(and is a read only file). Each MadLib worth is on a line, so how does one read text one line at a time?

Update: I don't think I was entirely clear. What I need to do is read a single line of multiple strings(CSV formatted) into an array, so is there a way to do that?

Replies are listed 'Best First'.
Re: Read from a file one line at a time?
by RichardK (Parson) on Feb 20, 2012 at 17:24 UTC

    Have a look at the File & I/O section of perlintro.

    perl comes with lots of documentation :)

Re: Read from a file one line at a time?
by choroba (Cardinal) on Feb 20, 2012 at 17:17 UTC
Re: Read from a file one line at a time?
by bitingduck (Deacon) on Feb 20, 2012 at 17:29 UTC

    Assuming you've already opened the filehandle FILE you can read all the lines into a list with

    my @lines=<File>;

    or read one line at a time with

    while (<FILE>){ my $line=$_; }

    there are more compact ways to do the latter, as well.

Re: Read from a file one line at a time?
by JavaFan (Canon) on Feb 20, 2012 at 18:23 UTC
    I was wondering if it is possible to read from a text file one line at a time
    Not only is that possible, it's also the default if read in scalar context. Of in a while (<$handle>). Or when using -p or -n.
Re: Read from a file one line at a time?
by GrandFather (Saint) on Feb 21, 2012 at 19:34 UTC

    Ah, your update contains the key "word": CSV. CPAN contains many modules for dealing with CSV, but a good starting point is Text::CSV.

    If you are new to Perl and possibly programming in general you'll probably hit a few bumps along the way. Come back with any problems you have and a runnable code sample showing the issue.

    True laziness is hard work

      Ah. Thanks. I'll try that then.

Re: Read from a file one line at a time?
by Anonymous Monk on Feb 24, 2012 at 21:09 UTC

    Here is a code snippet I use about 100 times a day:

    my $filename = 'pathToFile.txt'; # Use the 3 arg form of open. # Use 'my' to scope the file handle. # die if we cannot open the file, report error in '$!'. open my $input_fh, '<', $filename or die "Could not open $filename: $!"; # Loop through each line in the file. # Use 'chomp' to remove the trailing newline. ## Label your loops. If you nest loops like I do, ## your 'next' commands can reference your label to make ## sure you short circuit the correct loop. LINE: while (my $line = <$input_fh>) { chomp $line; # Do stuff here with $line... # And later maybe you test something and need to skip # to the next line: if ($something_happens) { next LINE; } }
Re: Read from a file one line at a time?
by Anonymous Monk on Feb 21, 2012 at 16:27 UTC

    I don't think I was entirely clear. What I need to do is read a single line of multiple strings into an array, so is there a way to do that?

      Sure. But what it is depends heavily on the format of the strings and how you want them in the array. Start with the other suggestions in this thread (for how to read each line), and try to figure out something. If/when that doesn't work, post what you've got and ask for help.

      We hate doing things for you here. We love to help though.

      Sorry. Was not logged in. My post