Re: commify_series script in cookbook page 94
- From: rob.dixon@xxxxxxx (Rob Dixon)
- Date: Mon, 31 Mar 2008 19:30:58 +0100
Dr.Ruud wrote:
Rob Dixon schreef:
Richard Lee wrote:
Dr.Ruud wrote:
Richard Lee schreef:
It's equivalent to:I understood the original but I am not so sure of your solution??While reading perl cookbook, I came to page 94 and having a hardShortcutting alternative:
time understanding this particular phrase
my $sepchar = grep( /,/ => @_ ) ? ";" : ",";
my $sepchar = ",";
for (@_) { /,/ and $sepchar = ";" and last }
but (unless @_ is quite big) the grep alternative is likely to be
faster.
Can you write it out completely so that I can try it out?
my $sepchar = ',';
foreach (@_) {
if (/,/) {
$sepchar = ';';
last;
}
}
And IMO is much better written that way.
TIMTOWTDI.
But I'm sure you'll agree that some ways are more awkward or obfuscated
than others.
Another shortcutting alternative:
my $sepchar = ',';
for (@_) { $sepchar = ";" and last if /\Q$sepchar/ }
This relies on ';' being true, and uses 'and' in void context.
$sepchar = '' and last if ...
wouldn't work; or, rather, it would work but wouldn't exit from the loop
when intended.
And another:
use List::Util qw(first);
my $sepchar = first( {/,/} @_ ) ? ";" : ",";
This relies on an element matching /,/ being true, and first()
unnecessarily returns the element value when all that is required is a
boolean.
And another:
use List::MoreUtils qw(any);
my $sepchar = any( {/,/} @_ ) ? ";" : ",";
Spot on. I believe that's the ideal solution as everything is being used
for its intended purpose. any() is required to return only a boolean
value, and it can be implemented so that it stops searching as soon as
the condition is satisfied, whereas grep is obliged to count right to
the bitter end. It's a shame that MoreUtils isn't a standard module.
But, as already stated, often this is fine:
my $sepchar = grep(/,/, @_) ? ";" : ",";
It is the usual way, yes. It has the huge advantage that it does what it
says it does (apart from grep being an awful name for a function).
Rob
.
- Follow-Ups:
- Re: commify_series script in cookbook page 94
- From: John W. Krahn
- Re: commify_series script in cookbook page 94
- From: Chas. Owens
- Re: commify_series script in cookbook page 94
- References:
- Interpolate variable in a __DATA__ block
- From: Trudge
- Re: Interpolate variable in a __DATA__ block
- From: Gunnar Hjalmarsson
- Re: Interpolate variable in a __DATA__ block
- From: Trudge
- Re: Interpolate variable in a __DATA__ block
- From: Chas. Owens
- commify_series script in cookbook page 94
- From: Richard Lee
- Re: commify_series script in cookbook page 94
- From: Dr.Ruud
- Re: commify_series script in cookbook page 94
- From: Richard Lee
- Re: commify_series script in cookbook page 94
- From: Rob Dixon
- Re: commify_series script in cookbook page 94
- From: Dr.Ruud
- Interpolate variable in a __DATA__ block
- Prev by Date: Re: opening sftp command as a file
- Next by Date: Re: parser using perl
- Previous by thread: Re: commify_series script in cookbook page 94
- Next by thread: Re: commify_series script in cookbook page 94
- Index(es):