Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^3: Perl modifying output of an array to remove blank lines

by AnomalousMonk (Archbishop)
on Oct 08, 2015 at 22:54 UTC ( [id://1144245]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Perl modifying output of an array to remove blank lines
in thread Perl modifying output of an array to remove blank lines

What is in your  @Goodlines array? The code posted by stevieb seems to work exactly as advertised:

c:\@Work\Perl>perl -wMstrict -MData::Dump -le "my @lines = ('', ' ', ' ', 'x', ' x', 'x ', ' x ', ' x ',); ;; my @no_empties = grep { $_ !~ /^(?:\s+|)$/ } @lines; ;; dd \@no_empties; " ["x", " x", "x ", " x ", " x "]
Can you put something into the  @lines array that doesn't come out as you want?

Incidentally, I would prefer an inverse approach to the regex: a non-blank line is one that has at least one non-blank (i.e., non-whitespace, or \S) character in it:

c:\@Work\Perl>perl -wMstrict -MData::Dump -le "my @lines = ('', ' ', ' ', 'x', ' x', 'x ', ' x ', ' x ',); ;; my @no_empties = grep m{\S}xms, @lines; ;; dd \@no_empties; " ["x", " x", "x ", " x ", " x "]


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^4: Perl modifying output of an array to remove blank lines
by hippo (Bishop) on Oct 08, 2015 at 23:37 UTC

    I humbly refer you to my previous reply which solves namelessjoe's problem. It works because the @goodlines array is empty on those occasions where the blank lines are output.

    In short, there's no point trying to remove some entries from the array which are themselves empty or just whitespace because there are no entries in the array. The thread title is a bit misleading in that respect. I initially started down the route which you and stevieb took, but a quick test determined that the output would not match what was reported in the original node. Hence the conclusion/supposition that the "bad" lines were ones where the array was entirely empty.

    Hope this helps and that it avoids anybody spending more time on blind alleys.

      Nice observation. Usually 'good post' type posts aren't presented here, but you had a good eye to suit the OP with what they were specifically asking.

      Short-circuiting definitely works better than iteration and removal (in the specific case presented).

      -stevieb

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1144245]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2024-03-28 21:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found