Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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. :)

In reply to Re: [Perl 6] List of length 2 or... by TimToady
in thread [Perl 6] List of length 2 or... by blazar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-25 07:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found