http://qs321.pair.com?node_id=45868

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

This post was moved from Categorized Questions and Answers
Please do not vote for this node. It will affect the wrong user.
Thank you -- Q&AEditors

I wish to populate an array from a text file.   Each line of text should be a list item.

Here's what I've come up with, but results in "Use of uninitialized value at line 11" error.   What am I missing?

1 #!/usr/bin/perl -w 2 use strict; 3 4 $file = "/path/file"; 5 open (<FH>, "< $file") or die "Can't open $file for read: $!"; 6 while (<FH>) { 7 push (@lines, $line); 8 } 9 close FH or die "Cannot close $file: $!"; 10 11 print join(',', @lines); # see if it worked

Replies are listed 'Best First'.
Re: How to populate array with lines from text file? (Moved from Q&A)
by Russ (Deacon) on Dec 09, 2000 at 10:47 UTC
    The main problem was $line. You should use $_ instead.

    After a few other clean-ups, this works:

    #!/usr/bin/perl -w use strict; my $file = "junk"; open (FH, "< $file") or die "Can't open $file for read: $!"; my @lines; while (<FH>) { push (@lines, $_); } close FH or die "Cannot close $file: $!"; print @lines; # see if it worked

    Russ
    Brainbench 'Most Valuable Professional' for Perl

Re: How to populate array with lines from text file? (Moved from Q&A)
by repson (Chaplain) on Dec 09, 2000 at 14:39 UTC
    Perl has an easy syntax for this, just use:
    open (FH, "< $file") or die "Can't open $file for read: $!"; my @lines = <FH>; close FH or die "Cannot close $file: $!";
    Since <FH> is in array context ie. the left hand side of the assignment expects a list of items, it will fill the array with all the lines in the file.

    A few other points, your open shouldn't have angle brackets, @lines and $file should be declared with my() (I don't know why you aren't getting a compile time error from strict) and $line should be $_ (if you want to continue with the while loop).
Re: How to populate array with lines from text file? (Moved from Q&A)
by Fastolfe (Vicar) on Dec 09, 2000 at 15:40 UTC
    I suspect the other posts will help you with your problem, but in the event they don't, it seems like you're omitting or heavily editing your code. What you posted shouldn't even compile, as you have variables that are undeclared. Typically you only get that message when working with an undefined scalar in such a way that you're assuming it has content. This can occur with your array if you have an undefined element, which is likely because you are pushing $line onto it, and I don't see anywhere where you are setting $line. So basically for each line that you read from the file (into $_), you're pushing 'undef' onto the array. Another poster mentioned that you should be using $_, which is correct, or change your while construct to read something like: while ($line = <FILE>) { ....

    So if these posts don't help you, can you post the real code with 2 or 3 additional lines so that we can get a better feel for context? Good luck.

Re: How to populate array with lines from text file? (thanks)
by ybiC (Prior) on Dec 09, 2000 at 18:55 UTC
    Thanks for the good quick answers, all.

    I was suffering a bad case of "find it in the book immediately after posting the question".   On page 92 of the Ram is, essentially, the advice y'all offered.   S'working now, t'sall's good.
        cheers,
        Don
        striving for Perl Adept
        (it's pronounced "why-bick")