Jason Balicki <[EMAIL PROTECTED]> wrote:

: I've got a phone record that keeps the date and time in the
: following format: 
: 
:           YYMMDDHHMM
: example:  0501201500
: 
: So, I've written the following to convert it to the format:
: 
:           MM/DD/YYYY,HH:MM
: example:  01/20/2005,15:00
: 
: 
: sub convertdate {
:         my($locdate)[EMAIL PROTECTED];
:         $ypfix=20;
:         @ardate = split(//,$locdate);
:         $year=join('',$ardate[0],$ardate[1]);
:         $month=join('',$ardate[2],$ardate[3]);
:         $day=join('',$ardate[4],$ardate[5]);
:         $hour=join('',$ardate[6],$ardate[7]);
:         $minute=join('',$ardate[8],$ardate[9]);
:         $jyear=join('',$ypfix,$year);
:         $jdate=join('/',$month,$day,$jyear);
:         $jtime=join(':',$hour,$minute);
:         $retdate=join(',',$jdate,$jtime);
:         open ( LOG, ">>$log" );
:         print LOG "$retdate";
:         close ( LOG );
: }
: 
: This works, but strikes me as ugly.  Is there a more
: elegant way of doing what I've done here?  It seems
: like I should be able to loop through the $ardate[n]
: entries and select them somehow instead of hard coding
: the indexes, but nothing is coming to mind.

    You have $locdate scoped to the sub and all the other scoped
outside the sub. You are also printing the new date into the log
instead of just returning the new date which is implied by the
sub name. $retdate should NOT be in quotes.


my $file = 'some_log_file';
open FH, ">>$file" or die qq(Cannot open "$file": $!);
    print FH log_date( $retdate );
close FH;

sub log_date {
    my $date = shift;

    my( $year, $month, $day, $hour, $minute ) = $date =~ /../g;

    return
        sprintf '%s/%s/20%s,%s:%s',
            $month, $day, $year, $hour, $minute;
}

HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
254 968-8328


-- 
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