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

$xronos = scalar(localtime(time + 7200)); $xronos =~ s/ 2004//; How can i cut the seconds from localtime with substitute operator like i did with the year? i could do it with $xronos =~ s/:dd 2004//; but its not working? give me some help here!

Replies are listed 'Best First'.
Re: Problem with s///
by antirice (Priest) on Jan 09, 2004 at 12:55 UTC

    Nik, check out perldoc perlre which will tell you all about the character classes including \d. To answer your question:

    $xronos =~ s/:\d{2} \d{4}//g;

    Of course, substitution isn't always the answer, you could simply do:

    substr($xronos,-8) = "";

    and get the same results.

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

      Thank you very much! indeed s/// is not always the answer! :)
        Ah, but the substr() solution will fail 7996 years from now. Code for posterity! s/:\d{2} \d{4,}\z//
Re: Problem with s///
by borisz (Canon) on Jan 09, 2004 at 14:14 UTC
    Somewhat unrelated to your question, but here is a example to get the seconds straight before they reach localtime.
    $time_str = localtime( int(time / 60) * 60);
    Boris
Re: Problem with s///
by Roger (Parson) on Jan 09, 2004 at 13:34 UTC
    You got the regex wrong, \d matches a digit, 'd' matches letter 'd'. The correct regex should be:

    $xronos =~ s/:\d\d 2004//;
Re: Problem with s///
by Roy Johnson (Monsignor) on Jan 10, 2004 at 04:16 UTC
    You might also consider POSIX strftime or using the list returned by localtime() if what you need is substantially different from the scalar output.

    The PerlMonk tr/// Advocate