His Perl could have been tightened up considerably.
Boy, is that a call to action! I can't believe I'm the first in 4 years to have taken it up …. Here's a more-or-less faithful copy:
use strict;
use warnings 'all';
use constant {
EMAIL => 17,
CONTACTME => 27,
SKUTITLE => 34
};
my @records; # Arrays are initialised empty; no need to do it explicit
+ly.
push @records, [ split "\t" ] for <>;
do { tr/"//d for @$_ } for @records;
my @contact_records;
do { push @contact_records, [ @$_[ SKUTITLE, CONTACTME, EMAIL ] ] if $
+_->[CONTACTME] == 1 } for @records;
{
local $, = "\t";
local $\ = "\n";
print @$_ for sort { $a->[0] cmp $b->[0] } @contact_records;
}
To me, this task suffers, rather than benefits, from the names, and is much better suited for a Unix-style pipeline; fortunately, Perl is good at those (although I resort to substr to avoid the to-me unsatisfying s///; $_):
use strict;
use warnings 'all';
use constant {
EMAIL => 17,
CONTACTME => 27,
SKUTITLE => 34
};
{
local $, = "\t";
local $\ = "\n";
print @$_ for
sort { $a->[0] cmp $b->[0] }
grep { $_->[1] }
map {
chomp;
[
map { substr $_, 1, -1 }
( split "\t" )[ SKUTITLE, CONTACTME, EMAIL ]
]
} <>;
}
I've replaced the test for whether the
CONTACTME field
== 1 (not
eq "1", ugh) with a test for whether it's true, since that's almost certainly what was meant; but it's easy to change it back.
Finally, note that, since the CONTACTME field is not carrying any useful information, we might as well avoid printing it; so we could define
sub snip_or_toss { $_[CONTACTME] ? @_[ SKUTITLE, EMAIL ] : () }
replace the outer
map by
map { chomp; [ map {...} snip_or_toss split "\t" ] }, and then get rid of the
grep.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.