What if Alex has three children? Who cares about shorter if it doesn't produce the right output.

use strict; use warnings; sub plural($@) { my ($n, $s, $p) = @_; if ($n == 0) { return "no $p"; } elsif ($n == 1) { return "one $s"; } else { return "$n $p"; } } sub count_phrase { my $last = pop @_; return $last if !@_; return join(', ', @_), ' and ', $last; } sub print_person { my ($person) = @_; my $name = $person->{name}; my $age = $person->{age}; my $children = $person->{children}; if ( @$children ) { my @chd_ages = map { $_->{age} } @$children; print("$name has ", count_phrase(@$children, 'child', 'children' +), "\n"); print("The are ", phrase_list(@chd_ages), " years old\n"); } else { print("$name has no children\n"); } print("$name is $age years old\n"); } { my %person = ( name => 'Alex', age => 30, children => [ { age => 2 }, { age => 5 }, ], ); print_person(\%alex); }

In reply to Re: short method to print age. by ikegami
in thread short method to print age. by zouz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.