in reply to removing empty lines

It doesn't quite look like your code matches your request; you don't seem to be removing empty lines, but skipping them.

If what you want to do is to ignore blank lines while reading a file, I would do something like this (note that this code is not tested:

open(my $infile, '<', $inputfile) or die "Could not open $inputfile be +cause $!\n"; while(<$infile>){ next if /^\s*$/; chomp; # you don't want the newline, do you? #processing follows here... } close($infile); }

I'm defining "blank line" to be any line comprising only whitespace characters.

Note that tr or y does not handle character classes (to quote: "Note that tr does not do regular expression character classes such as \d or [:lower:]"; see perlop)


(note that most Monks are paid to review people's code; some pleasant words like "please" would be nice when asking for free services).

emc

Insisting on perfect safety is for people who don't have the balls to live in the real world.

—Mary Shafer, NASA Dryden Flight Research Center

Replies are listed 'Best First'.
Re^2: removing empty lines
by mikejones (Scribe) on May 18, 2007 at 17:38 UTC
    Ok thank you! So tr does not do all regexp metachars such as \d \D \s +\S \w \W? is \s+ is whitespace, why didn't s/^\s+|\s+$//g work on these empty li +nes? So \S is non-whitespace so essentially it is ???
      Your substitute command is only operating on a single line. After it has removed all characters from the line, you are still left with a blank line. The "next if blank" approach works best for removing unwanted lines in my opinion.

      /^\s+$/ or /\s+$/ matches one or more whitespace characters; neither will match an empty line, i.e., one consisting solely of a newline (as would a line printed by print "\n";) won't be matched.

      emc

      Insisting on perfect safety is for people who don't have the balls to live in the real world.

      —Mary Shafer, NASA Dryden Flight Research Center