I've come around to the opinion that the desire to add extra adverbs or arguments should usually be taken as a design smell that you're trying to use the wrong tool for the job. In this case, I think Perl 5 programmers have a bit of unlearning to do, since
split has become one of those all-you-have-is-a-hammers that gets used inappropriately on various everything-is-a-nails because of the absence of its figure/ground counterpart,
comb.
In Perl 6 you should use whichever one is more appropriate and readable. So if I didn't mind slurping
the whole file, I'd probably just say:
my %hash = $fh.slurp.comb(/ ^^ (\T*) \t (\N*) /);
though perhaps a Perl 5 programmer would be more comfortable with the implicit iteration of a global regex:
my %hash = $fh.slurp ~~ m:g/ ^^ (\T*) \t (\N*) /;
But I think the explicit use of
.comb is more readable.
If I didn't want to slurp the file, I'd use the convenient .lines method instead of the generic but cryptic prefix:<=> operator. I might write some kind of list comprehension:
my %hash = ($0,$1 if / ^^ (\T*) \t (\N*) / for $fh.lines);
or maybe just the same thing written as a normal
for loop:
my %hash = do for $fh.lines {
/ ^^ (\T*) \t (\N*) / and $0,$1;
}
Or if I wanted to emphasize the data flow, I might use an explicit
gather/
take construct:
my %hash = gather for $fh.lines {
take / ^^ (\T*) \t (\N*) / || ();
}
I guess there's still more than one way to do it in Perl 6.
:)
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.