Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Sentances from arrays

by IndyZ (Friar)
on Nov 28, 2000 at 03:11 UTC ( [id://43560]=perlquestion: print w/replies, xml ) Need Help??

IndyZ has asked for the wisdom of the Perl Monks concerning the following question:

I have an array like:
@array = ("12 hours", "23 minutes", "17 seconds");
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:
$last = pop(@array); $sentence = join ", ", @array; $sentence .= "," if ($#array > 0); $sentence .= " and " if ($#array > -1); $sentence .= "$last";
This works for anything, except zero elements, which I check for earlier in the block.

I'm sure some of you wizards can see what I cannot. Any help is appreciated.

=IndyZ

Replies are listed 'Best First'.
Re: Sentances from arrays
by merlyn (Sage) on Nov 28, 2000 at 03:24 UTC
    It's probably clearer if you just say what you mean:
    $sentence = @array <= 0 ? "" : @array <= 1 ? $array[0] : @array <= 2 ? join(" and ", @array) : join(", ", @array[0..$#array-1], "and $array[-1]");
    That's clearer to me, and safe in case you delete or add a case.

    -- Randal L. Schwartz, Perl hacker

Re: Sentances from arrays
by myocom (Deacon) on Nov 28, 2000 at 03:33 UTC
    Here's what I use:
    sub list_commify { if (@_ > 2) { join ", ", @_[0..$#_-1], " and $_[-1]" } elsif (@_ > 1) { "$_[0] and $_[1]" } elsif (@_ > 0) { $_[0] } else { "" } }
      Personally, I don't like if's. In my mind, they make code harder to read by forcing you to think your way through them. While they are appropriate in other cases, in this case I believe merlyn is clearer.

      =IndyZ

        To each his own, natch. For me, at least, it's easier to parse the if's, since it's more English-like. I can parse it by reading it aloud.

        I find it harder to pronounce the ? : version, though I appreciate its elegance.

Re: Sentances from arrays
by cwest (Friar) on Nov 28, 2000 at 05:40 UTC
    my $sentence = join @array > 2 ? ', ' : ' ', @array > 1 ? ( @array[0..$#array-1], 'and '.$array[-1] +) : @array;
    --
    Casey
       I am a superhero.
    
(2501) Re: Sentances from arrays
by 2501 (Pilgrim) on Nov 28, 2000 at 21:15 UTC
    I couldn't help it...it looked like too much fun trying to crunch this code to its smallest form:P
    use strict; my @foo = ("23 hours","22 minutes","21 seconds","20 miliseconds","0 na +noseconds"); my $final; $final?($final =pop(@foo).", $final"):($final =($#foo>1)?"and ".pop(@f +oo):pop(@foo)) until not @foo; print "$final\n";


    update: I removed an extra space in the output string and a wee mistake (it functioned correctly but it was useless code) from a previous attempt

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://43560]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-26 09:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found