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

Review this code: Goal is to remove empty lines only, not the |. thank you
use strict; use warnings; use Data::Dumper; my $test = qq(/etc/skel/test); my ($id,$name,$hash); open (my $test1, "+<", $test) or die "file '$test' was not opened $!"; my (%HoA,$HoA); while(<$test1>) { s/^\s+|\s+$//g; y/^\S|\S$//d; ($id,$name)=split(/\|/,$_); push @{$HoA{$id}}, $name; } print Dumper(\%HoA); for my $i (sort keys %HoA) { print "$i -- @{ $HoA{$i} }\n"; } __DATA__ $ cat -etu test 1|Michael$ 1|Alex$ 1|Bob$ 1|Pete$ 2|Bob$ 2|Andre$ 2|David$ 2|Alex$ 3|Pete$ $ $

Replies are listed 'Best First'.
Re: removing empty lines
by swampyankee (Parson) on May 18, 2007 at 16:15 UTC

    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
      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