in reply to Re: string concatenation
in thread string concatenation

Hi, The actual code is like this: ($min, $hours) = localtime(2,3); $now = "$hour" . "$min" # expect to see - $now = "1208" For minutes equal or greater than 2 digits, it's fine. Only when minutes is less than 2 digits ( < 10). Any help is appreciated.

Replies are listed 'Best First'.
Re: Re: Re: string concatenation
by Enlil (Parson) on Nov 13, 2002 at 00:20 UTC
    The problem is that $min is returned from localtime as "2" instead of "02", the same with hours for that matter you could do something like the following:
    my ($min,$hours) = (localtime)[2,3]; my $now = $hours . sprintf ("%02",$min);

    -enlil

      Thanks! EnLil. That works great.
Re: Re: Re: string concatenation
by DamnDirtyApe (Curate) on Nov 13, 2002 at 02:04 UTC

    Enlil beat me to the sprintf solution, but if you've got more complex date formatting to do, consider using the strftime function from the POSIX module.

    use POSIX ; my $now = POSIX::strftime( "%H%M", localtime ) ; print $now, "\n" ;

    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche