IndyZ has asked for the wisdom of the Perl Monks concerning the following question:
I am trying to turn this into an English sentence like "12 hours, 23 minutes, and 17 seconds" Not so tough right? The real problem is that I never know how many items are in the list. The best that I could come up with was:@array = ("12 hours", "23 minutes", "17 seconds");
This works for anything, except zero elements, which I check for earlier in the block.$last = pop(@array); $sentence = join ", ", @array; $sentence .= "," if ($#array > 0); $sentence .= " and " if ($#array > -1); $sentence .= "$last";
I'm sure some of you wizards can see what I cannot. Any help is appreciated.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Sentances from arrays
by merlyn (Sage) on Nov 28, 2000 at 03:24 UTC | |
|
Re: Sentances from arrays
by myocom (Deacon) on Nov 28, 2000 at 03:33 UTC | |
by IndyZ (Friar) on Nov 28, 2000 at 04:56 UTC | |
by myocom (Deacon) on Nov 28, 2000 at 05:01 UTC | |
|
Re: Sentances from arrays
by cwest (Friar) on Nov 28, 2000 at 05:40 UTC | |
|
(2501) Re: Sentances from arrays
by 2501 (Pilgrim) on Nov 28, 2000 at 21:15 UTC |