The map and pop suggestions work (very nice)... The $_ trick wont though, because (get this)
$_ isn't actually empty at that point, it contains a single newline. Interestingly enough /^$/ will match a single newline.
perl -le '$_ = "\n"; print /^$/'
1
So, a tested version of your code comes in at 69 chars....
# 1 2 3 4 5 6
#23456789012345678901234567890123456789012345678901234567890123456789
map{$n=$o=$_;for$c(@_){$n+=s/$c//}/^$/&&push@{$w[$n]},$o}<D>;@{pop@w}
Update:
The following code illustrates the issue a bit better...
Notice how
/^$/ && is not identical to
$_ || and in this example the difference is critical:
#!/usr/bin/perl -wT
use strict;
my @arr = ("\n","dog\n","cat\n");
my $patternmatch = 0;
my $underscorematch = 0;
/^$/ && $patternmatch++ for @arr;
$_ || $underscorematch++ for @arr;
print "Patternmatch = $patternmatch\n"; # <== prints 1
print "Underscorematch = $underscorematch\n"; # <== prints 0
-Blake
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.