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:
- "specify the return list length," much like the current third parameter;
- specify how to fill it in case splitted items are not enough, either in a dwimmy way or by some list construction method - and we've seen quite cool ones lately.
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?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: [Perl 6] List of length 2 or...
by TimToady (Parson) on Oct 22, 2008 at 16:24 UTC | |
by Anonymous Monk on Oct 23, 2008 at 10:57 UTC | |
by moritz (Cardinal) on Oct 23, 2008 at 11:02 UTC | |
Re: [Perl 6] List of length 2 or...
by kyle (Abbot) on Oct 22, 2008 at 14:56 UTC | |
by blazar (Canon) on Oct 23, 2008 at 18:29 UTC | |
Re: [Perl 6] List of length 2 or...
by moritz (Cardinal) on Oct 22, 2008 at 14:59 UTC | |
Re: [Perl 6] List of length 2 or...
by Jenda (Abbot) on Oct 22, 2008 at 23:37 UTC | |
Re: [Perl 6] List of length 2 or...
by ikegami (Patriarch) on Oct 22, 2008 at 16:53 UTC | |
by moritz (Cardinal) on Oct 22, 2008 at 16:58 UTC |