commify_series script in cookbook page 94
- From: rich.japh@xxxxxxxxx (Richard Lee)
- Date: Sun, 30 Mar 2008 17:55:49 -0400
While reading perl cookbook, I came to page 94 and having a hard time understanding this particular phrase
my $sepchar = grep( /,/ => @_ ) ? ";" : ",";
I recognize the ternary operator and grep but I am not sure how they are forming the meaning together.
I thought grep needed lists to work on ? and grep(/,/ => @_), I am not sure where it's getting the list from
my @results = grep EXPR, @input_list;
my $count = grep EXPR, @input_list;
And I also don't understand what ";" is doing in the ternary operator??
I think I understand the rest of the program though..
Can someone help me out please?
thank you.
-------------------- Complete program --------------------------
#!/usr/bin/perl -w
use strict;
my @lists = (
[ 'just one thing' ],
[ qw(Mutt Jeff) ],
[ qw(peter Paul mary) ],
[ 'To our parents', 'Mother Theressa', 'God' ],
[ 'pastrami', 'ham and cheese', 'peanut butter and jelly', 'tuna' ],
[ 'recycle tired, old phrases', 'ponder big, happy thoughts' ],
[ 'recycle tired, old phrases',
'ponder big, happy thoughts',
'sleep and dream peacefully' ],
);
for my $aref ( @lists ) {
print "The list is: ". commify_series( @$aref ) . ".\n";
}
sub commify_series {
my $sepchar = grep( /,/ => @_ ) ? ";" : ",";
( @_ == 0 ) ? ''
: ( @_ == 1 ) ? $_[0]
: ( @_ == 2 ) ? join(" and ", @_ )
: join("$sepchar ", @_[0 .. ($#_-1) ], "and $_[-1]" );
}
.
- Follow-Ups:
- Re: commify_series script in cookbook page 94
- From: Dr.Ruud
- Re: commify_series script in cookbook page 94
- From: Rob Dixon
- Re: commify_series script in cookbook page 94
- From: Yitzle
- Re: commify_series script in cookbook page 94
- From: John W. Krahn
- 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
- Interpolate variable in a __DATA__ block
- Prev by Date: Re: Interpolate variable in a __DATA__ block
- Next by Date: Re: commify_series script in cookbook page 94
- Previous by thread: Re: Interpolate variable in a __DATA__ block
- Next by thread: Re: commify_series script in cookbook page 94
- Index(es):