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

Aloha, I've worked on it and got it to work. I appreciate davidrw tone, after all I'm just humbly trying to learn. Mahalo for everyones patience! Joseph W. Guillaume
#!/usr/bin/perl -Tw use strict; use CGI qw(:standard); print "Content-Type: text/html\n\n"; print '<body bgcolor="cccccc">'; ##################################### my $employee=param('employee_name'); my $certification=param('certification'); my $month=param('month'); my @month=""; my $day=param('day'); my $year=param('year'); my $key=""; my $i=""; my $t=""; my @data=""; my $items; my $lines; my $number; my @mon = qw (January Feburary March April May June July August Septem +ber October November December); ##################################### # Read File # ##################################### print "Employees name is <b> $employee </b><p\/><hr><p\/>"; open(JOE, "<Files/$employee.txt"); @data = <JOE>; close (JOE); ##################################### # Print File # ##################################### $lines=scalar@data/4; for ($i=1; $i<=$lines; $i++) { $certification = shift(@data); print "Certification is $certification on "; $number = shift (@data); $month = $mon [$number-1]; print "$month"; $day = shift (@data); print " $day"; $year = shift(@data); print " $year"; print "<p\/><HR>"; } ##################################### print '<div align="center"><table width="120" border="1" cellspacing=" +2" cellpadding="0"><tr><td align="center">To go Back</td></tr><tr><td + align="center"><a href="http://hawaiicivilmarriage.com/FFD/">Click H +ere</a></td></tr></table><p></p></div>'; print '</body'; ##################################### exit;

Replies are listed 'Best First'.
Re: Change number into month
by ikegami (Patriarch) on May 24, 2005 at 20:29 UTC

    There's shouldn't be a loop in the sub month. In fact, something is obviously wrong since $damonth isn't used. Just replace
    &month;
    with
    $month = $mon{$damonth};
    or maybe
    $month = $mon{0+$damonth};
    for it to work.

    I'm wondering what you aren't using an array:

    my @mon = qw( January Feburary March April May June July August September October November December ); ... $month = $mon[$damonth-1];

    And please put <code> tags around your code when you post here. It makes your code easier to read, and you don't have to add a bunch of <br> tags or escape stuff like <.

Re: Change number into month
by davidrw (Prior) on May 24, 2005 at 21:14 UTC
    Here are some random hint/tips that might not be directly for the core question, but will help in general:
    • Your creation of %mon will work just fine, but it's general practice to use the => in place of some of the commas in order to explictly denote the key => value nature of the relationship. (But as already noted, an array is probably better here anyways.)
      my %mon = ( 1 => 'January', 2 => 'Feburary', 3 => 'March', 4 => 'April +', 5 => 'May', 6 => 'June', 7 => 'July', 8 => 'August', 9 => 'Septemb +er', 10 => 'October', 11 => 'November', 12 => 'December');
    I had some other comments/tips, but the original post/code has been accidentally overwritten.. I'll follow-up if/when it's restored.
Re: Change number into month
by davidrw (Prior) on May 25, 2005 at 14:47 UTC
    glad you got it working! I'll continue where i was going above with some general hints/guidelines:
    • Try to keep output statements separate from the logic. For example, instead of alternating code & print statements, just do a single print at the bottom of the 'for' loop:
      printf 'Certification is %s on %02d % 02d %d<p><HR>', $certification, $month, $day, $year;
      Kind of trivial here, but what this will do is make the output much easier to understand and modify in the future. (For larger future projects, be sure to investigate things like Template::Toolkit or HTML::Template)
    • Be aware of the scope of your variables. In this case, $i, $certification, $number, $month, $day, and $year are all only used inside the for loop. so instead of having the 'my' declaration globally, can just do it in the for loop (this makes it very clear that these variables only exist/have meaning just right here):
      for (my $i=1; $i<=$lines; $i++) { my $certification = shift(@data); my $number = shift (@data); my $month = $mon [$number-1]; my $day = shift (@data); my $year = shift(@data); # printf .... }
    • this is a great one for TMTOWTDI .. first off, note that the for ($i=1; $i<=$lines; $i++) construct is very "c-ish" and there are usually 'better' ways to write it in perl.. For example (there's no "right" way here--just a bunch of different ways) the $i is unnecessary in this case:
      while(@data){ ... }
      Note that it's still dependent on the file format being sets of 4 lines ..
    • One of perl's great strength's is CPAN -- so always remember that if the answer to "is it likely someone else has had to do this?" is "yes" or "most certainly", head to http://search.cpan.org. For example, Date::Calc (there are other Date modules as well) has a Month_to_Text function that does exactly what you need, thus eliminating the need for @mon or %mon or something like that.
      use Date::Calc qw/Month_to_Text/; while(@data){ ... my $number = shift (@data); my $month = Month_to_Text($number); ... }
Re: Change number into month
by mifflin (Curate) on May 25, 2005 at 04:27 UTC
    C:\tmp>type test.pl use POSIX; for my $i (0 .. 11) { print strftime( "%B", 0,0,0,1,$i,0),"\n"; } C:\tmp>test.pl January February March April May June July August September October November December
Re: Change number into month
by davidrw (Prior) on May 25, 2005 at 16:15 UTC
    ok, last post for me on this thread :)
    Here's a quick rewrite to illustrate some above the above points (and a few extras, like the hash-slice/map/chomp/splice line).. the Template usage is probably overkill, but a good example if this is going to move to a larger project.. (again, this isn't "right" or yours "wrong" just different ways to do stuff--which makes this fun and why i went nuts on this thread :) )