Yesterday I hinted in two different replies to what is basically the same question (how to "read pairs from a file, in a situation that's natural to be handled by split, and put them into a hash?") to the fact that whichever technique one adopted, she should sanitize the pair she got in case it is not actually a pair. I also hinted to the fact that the techinque I "chose" is more sensible to such an inconvenience: there are tons of ways to sanitize it in Perl 5 already. One that I'm certain some people would disagree with but that I would probably choose is:

my %pairs = map { chomp; map @$_ == 2 ? @$_ : (), [split /\t/, $_, 2] +} <FH>;

if I just wanted to ignore "broken" records, or maybe, if I wanted to supply defaults for missing keys:

my %pairs = map { chomp; map @$_ == 2 ? @$_ : (@$_ == 1 ? $_->[0] => undef : ()), [split /\t/, $_, 2] } <FH>;

or possibly written like

my %pairs = map { chomp; map @$_ == 2 ? @$_ : (@$_ == 1 ? @$_, undef : ()), [split /\t/, $_, 2] } <FH>;

depending on the psychological feeling I want to convey...

In all three examples what bothers me most even if I'm far from being a premature optimization kinda guy is to "have" to take a reference only to dereference it: it strikes me as doing something unnecessary... but I like the conceptual terseness, especially of the first, while the latter ones begin to be visually obtrusive...

Whatever: I wonder whether Perl 6 provides a means to handle such situations in an even more clear, concise and syntactically sweet manner. Now that I'm about to terminate writing the post it occurs to me that in this particular case "the" solution may be in terms of a pair of adverbs for split which would respectively:

But what in more general situations?

One last minute thought: one characteristic of English I've always envied e.g. to my own mother tongue, i.e. Italian, is the ease with which one part of the discourse can be transformed into another one. For example from noun to adjective, and so on. Now I wonder whether in this vein a list could be made equivalent to the action of taking a list and thus whether it would be possible to apply an adverb to it, as in the paragraph above. Or am I just brainstorming too much, and perhaps inconsistently?

--
If you can't understand the incipit, then please check the IPB Campaign.

In reply to [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":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.