I'm a little confused as to the purpose of this snippet. Are records in your file separated by an extra newline? I ask because it *appears* that your code tests each line in @lines to see if it only contains "\n". If so, it pops the last line off of the array (probably not what you want), decreases the number of lines expected (probably what you want) and decrements the loop index (which will cause the loop to hit the newline again, and keep popping off good lines from your array, probably the source of your error message).

If you want to get rid of blank lines more easily, one idea is:

#!C:\Perl\bin\perl.exe -w use strict; # I'm a masochist my $file = shift || 'foo.txt'; # why not my @lines; open(SESAME, $file) || die "Cannot open $file: $!"; # save debuggin +g time while (<SESAME>) { chomp; next unless $_; push @lines, $_; } close(SESAME) || die "Cannot close $file: $!"; my $total = @lines; print "There are $total records in $file\n";
Not too bad. I can make it shorter, though:
{ local *SESAME; # localizing a filehandle saves debugging ti +me too open (SESAME, $file) || die "Foo: $!"; local $/ = "\n\n"; # "line" delimiter is now two newlines @lines = <SESAME>; close (SESAME) || die "Bar: $!"; }
Bingo! One record per line in the array. You could also throw a split in there on newlines if you wanted records stored in the array one line at a time.

In reply to Re: I don't understand why I'm getting an unitialized value warning by chromatic
in thread I don't understand why I'm getting an unitialized value warning by little_mistress

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.