Sample perl code does not work (from Perl cookbook, 2nd ed.)
From: Staale (ksflock.takeaway_at_yahoo.com)
Date: 09/30/04
- Next message: Gunnar Hjalmarsson: "Re: Sample perl code does not work (from Perl cookbook, 2nd ed.)"
- Previous message: 187: "Re: Odd sort() problem"
- Next in thread: Gunnar Hjalmarsson: "Re: Sample perl code does not work (from Perl cookbook, 2nd ed.)"
- Reply: Gunnar Hjalmarsson: "Re: Sample perl code does not work (from Perl cookbook, 2nd ed.)"
- Reply: A. Sinan Unur: "Re: Sample perl code does not work (from Perl cookbook, 2nd ed.)"
- Reply: Sid Norman: "Re: Sample perl code does not work (from Perl cookbook, 2nd ed.)"
- Reply: Paul Lalli: "Re: Sample perl code does not work (from Perl cookbook, 2nd ed.)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 30 Sep 2004 00:41:07 +0200
Howdy all.
Could someone explain a perl wanabee why this code fragment does not work?
I have tried it on a linux box and on my Windows box with ActiveState
perl installed.
#---- code start
#!/usr/bin/perl -w
# commify_series - show proper comma insertion in list output
# @lists is an array of (references to anonymous) arrays
@lists = (
[ 'just one thing' ],
[ qw(Mutt Jeff) ],
[ qw(Peter Paul Mary) ],
[ 'To our parents', 'Mother Theresa', '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' ],
);
foreach $aref (@lists) {
print "The list is: " . commify_series(@$aref) . ".\n";
}
# demo for single list
@list = qw(one two three);
print "The last list is: " . commify_series(@list) . ".\n";
sub commify_series {
my $sepchar = grep(/,/ => @_) ? ";" : ",";
(@_ = = 0) ? '' :
(@_ = = 1) ? $_[0] :
(@_ = = 2) ? join(" and ", @_) :
join("$sepchar ", @_[0 .. ($#_-1)], "and $_[-1]");
}
#--- Code end
Best Regards
Staale Flock
Norway.
-- To mail me takeaway the obvious..:o) ksflock.takeaway@yahoo.com
- Next message: Gunnar Hjalmarsson: "Re: Sample perl code does not work (from Perl cookbook, 2nd ed.)"
- Previous message: 187: "Re: Odd sort() problem"
- Next in thread: Gunnar Hjalmarsson: "Re: Sample perl code does not work (from Perl cookbook, 2nd ed.)"
- Reply: Gunnar Hjalmarsson: "Re: Sample perl code does not work (from Perl cookbook, 2nd ed.)"
- Reply: A. Sinan Unur: "Re: Sample perl code does not work (from Perl cookbook, 2nd ed.)"
- Reply: Sid Norman: "Re: Sample perl code does not work (from Perl cookbook, 2nd ed.)"
- Reply: Paul Lalli: "Re: Sample perl code does not work (from Perl cookbook, 2nd ed.)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]