Charlotte Hee wrote:

In my perl script I use the unix command 'date' to make a time stamp because eventually I want the date format to be DD-MON-YYYY, where DD is the number of the day (1-31), MON is the 3-char spelling of month (i.e. JAN), and YYYY is the 4-digit year. In this perl script I'm just testing for the date so the day and year aren't in this script. The date command returns the month as a two digit number which I pass to my routine. My routine then changes the two digit number into the corresponding 3-char letters for that month. I get an error message like

   Illegal octal digit '8'
   Illegal octal digit '9'

when my routine uses the number 08 and 09. I don't understand what
this error means. If I change my routine to use 8 and 9 I don't see this
error message. The number for April is 04 and that doesn't seem to cause a
problem. What is special about numbers 08 and 09?


In general this is a bad way of going about all of this, but we will get to that in a moment...



Here is the code: ( thanks, Chee )


#!/usr/local/bin/perl -w #

use strict;

A good start...


use vars qw($mm $mon);


perldoc -f our 'use vars is a bit dated, unless you know why to use them or have an older Perl, stick to 'our'. Having said that do they need to be package variables in the 'main' namespace? Better to make them lexically scoped with 'my'


perldoc -f my

$mm = `date +%m`;

There is no reason to shell out to get the date components *especially* if you aren't going to use full paths, chomp the output, check for error codes, etc.


perldoc -f localtime

$mon = &convertMO($mm);


Drop the & until you know why you don't.


print "month is $mon\n";

exit;

sub convertMO {
    my ($tmp) = @_;
    my $xmon;

    if ( $tmp == 01 ){ $xmon = 'JAN'; }
    if ( $tmp == 02 ){ $xmon = 'FEB'; }
    if ( $tmp == 03 ){ $xmon = 'MAR'; }
    if ( $tmp == 04 ){ $xmon = 'APR'; }
    if ( $tmp == 05 ){ $xmon = 'MAY'; }
    if ( $tmp == 06 ){ $xmon = 'JUN'; }
    if ( $tmp == 07 ){ $xmon = 'JUL'; }
    if ( $tmp == 08 ){ $xmon = 'AUG'; }  #<-- illegal error
    if ( $tmp == 09 ){ $xmon = 'SEP'; }  #<-- illegal error

Since you are using the numerical comparison on numbers that start with a '0' which Perl takes to mean as numbers in octal rather than decimal. As soon as you hit 8 and 9 the digits are not expected in an octal number. You would be better off using string comparison, or dispensing with this subrouine anyways. It would be much simpler and faster to store the month abbreviations in a hash and index into that hash using a key, better still use an array since it is already ordered with numbered indexes.



    if ( $tmp == 10 ){ $xmon = 'OCT'; }
    if ( $tmp == 11 ){ $xmon = 'NOV'; }
    if ( $tmp == 12 ){ $xmon = 'DEC'; }

    return $xmon;
}



So a Perlish way to do this,

my (@months) = ('jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec');
my ($mday, $mon, $year) = (localtime)[3,4,5];


my $date = sprintf('%02d-%3s-%04d', uc $mday, $months[$mon] , $year+1900);

print "Date: $date\n";

perldoc -f uc
perldoc -f sprintf

HTH,

http://danconia.org

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to