Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Your perseverence wins plaudits. + +, Lady A!

FWIW, it's only a minor mod to make your program accept month_names. Wisdom says, do it with the capabilities already written into Date::Calc and tested repeatedly. Curiousity said, 'Do something different!'

Hence, this derivative - which is basicly your code with minor mods and a hash as the heart of the conversion (for all I know, this is what happens inside use Date::Calc qw/Decode_Month/ but the night's too far gone to check that):

#!/usr/bin/perl use strict; use warnings; use 5.012; # Lady Aleena's # 927621 use Data::Dumper; use Date::Calc qw(:all); use Lingua::EN::Inflect qw(ORD); #local $\ = "\n"; #commenting this out puts data entry on same li +ne as the relevant prompt; personal pref sub commify { local $_ = shift; 1 while s/^([-+]?\d+)(\d{3})/$1,$2/; return $_; } say "\n\tUsage: 4 digit year, Month as name -* OR *- as month_number, +1-12, day_of_month as number.\n"; print "What year were you born? "; chomp (my $birth_year = <>); print "What month were you born? "; chomp (my $birth_month = <>); print "What day were you born? "; chomp (my $birth_day = <>); if ( $birth_month =~ /[A-Z]+/i ) { $birth_month = convert($birth_month); # NOTE 1 in head, below } my $year = (localtime)[5] + 1900; my $month = (localtime)[4] + 1; my $day = (localtime)[3]; my @birth = ($birth_year,$birth_month,$birth_day); my @today = ($year,$month,$day); # Just figuring how many birthdays you have had. my $birthdays; if ($birth_month > $month) { $birthdays = $year - $birth_year - 1; } else { if ($birth_day > $day) { $birthdays = $year - $birth_year - 1; } else { $birthdays = $year - $birth_year; } } my $days_alive = Delta_Days(@birth,@today); my $unbirthdays = ORD(commify($days_alive - $birthdays)); print "Happy $unbirthdays unbirthday!"; sub convert # named months to Date::Calc numeric months { my $word_month=$_[0]; my %months = ('jan' => 1, # NOT zero-based - Date::Calc forma +t 'feb' => 2, 'mar' => 3, 'apr' => 4, 'may' => 5, 'jun' => 6, 'jul' => 7, 'aug' => 8, 'sep' => 9, 'oct' => 10, 'nov' => 11, 'dec' => 12); my ($key, $value); while (($key, $value) = each(%months)) { if ( $key =~ /$word_month/i ) { return($value) } else { next; } } } =head NOTE 1 # Conversion with the sub here is for illustration only; in practice, +it's better done with '$birth_month=Decode_Month($Birth Month);' cf perl -e "use Date::Calc qw/Decode_Month/; $mon='Dec'; $mon_name = Deco +de_Month($mon); print '\$mon_name: ' . $mon_name;" \$mon_name: 12 =cut

Now, a fairly trivial challenge -- chiefly for newcomers, please -- Add error checking on the data entered for birth_year and birth_month... and for bonus points, code or narrative description of other enhancements. The effort may put you on the path Lady_A has followed with such success.


In reply to Re: Happy unbirthday! (To all, with a challenge) by ww
in thread Happy unbirthday! by Lady_Aleena

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (7)
    As of 2020-10-22 17:42 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      My favourite web site is:












      Results (229 votes). Check out past polls.

      Notices?