in reply to I don't understand why I'm getting an unitialized value warning
If you want to get rid of blank lines more easily, one idea is:
Not too bad. I can make it shorter, though:#!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";
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.{ 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: $!"; }
|
|---|