Re: commify_series script in cookbook page 94
- From: rvtol+news@xxxxxxxxxxxx (Dr.Ruud)
- Date: Mon, 31 Mar 2008 19:32:10 +0200
Rob Dixon schreef:
Richard Lee wrote:
Dr.Ruud wrote:
Richard Lee schreef:
While reading perl cookbook, I came to page 94 and having a hard
time understanding this particular phrase
my $sepchar = grep( /,/ => @_ ) ? ";" : ",";
Shortcutting alternative:
my $sepchar = ",";
for (@_) { /,/ and $sepchar = ";" and last }
but (unless @_ is quite big) the grep alternative is likely to be
faster.
I understood the original but I am not so sure of your solution??
Can you write it out completely so that I can try it out?
It's equivalent to:
my $sepchar = ',';
foreach (@_) {
if (/,/) {
$sepchar = ';';
last;
}
}
And IMO is much better written that way.
TIMTOWTDI.
Another shortcutting alternative:
my $sepchar = ',';
for (@_) { $sepchar = ";" and last if /\Q$sepchar/ }
And another:
use List::Util qw(first);
my $sepchar = first( {/,/} @_ ) ? ";" : ",";
And another:
use List::MoreUtils qw(any);
my $sepchar = any( {/,/} @_ ) ? ";" : ",";
But, as already stated, often this is fine:
my $sepchar = grep(/,/, @_) ? ";" : ",";
--
Affijn, Ruud
"Gewoon is een tijger."
.
- Follow-Ups:
- Re: commify_series script in cookbook page 94
- From: Rob Dixon
- 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
- Interpolate variable in a __DATA__ block
- Prev by Date: Re: parser using perl
- Next by Date: Re: sort without ignoring hyphens
- Previous by thread: Re: commify_series script in cookbook page 94
- Next by thread: Re: commify_series script in cookbook page 94
- Index(es):