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

Hello,

in a file are records of the following format:

----0---m---28.07.2007---23:13:18---rest---id=123456 ----9---a---28.07.2007---23:13:46---rest---id=345677 ----0---a---28.07.2007---23:13:57---rest---id=876543
By comparing the id's to the actual processing id (e.g. id=345677 ), I'd like to increase the counter here from 9 to 10 and set the new timestamp. By increasing the counter there is to notice that the new counter-value has 2 digits.
The record in the file should look after the processing like this:
---10---a---29.07.2007---13:32:46---rest---id=345677
What is the best way to do the whole processing ?

Here is my code for defining the actual timestamp

### timestamp################################## ( $sec ,$min ,$hour ,$mday ,$mon ,$year ) = localtime; $jahr = 1900+$year; if ( $mon <= 9 ) {$mon = "0"."$mon";} if ( $mday <= 9 ) {$mday = "0"."$mday";} if ( $sec <= 9 ) {$sec = "0"."$sec";} if ( $min <= 9 ) {$min = "0"."$min";} if ( $hour <= 9 ) {$hour = "0"."$hour";} ### end timestamp##################################

Replies are listed 'Best First'.
Re: process record in file
by FunkyMonk (Bishop) on Aug 29, 2007 at 10:36 UTC
    Have a look at sprintf for formatting numbers. The rest is just substitutions:
    while ( <DATA> ) { next unless m/id=345677/; my ( $count ) = /(\d+)/; ( substr($_, 0, 5) = sprintf( "%5d", $count + 1 ) ) =~ s/ /-/g; my ( $sec ,$min ,$hour ,$mday ,$mon ,$year ) = localtime; my $date = sprintf "%02d.%02d.%4d", $mday, $mon, $year + 1900; my $time = sprintf "%02d:%02d:%02d", $hour, $min, $sec; s/\d\d\.\d\d\.\d\d\d\d/$date/; s/\d\d:\d\d:\d\d/$time/; print; } __DATA__ ----0---m---28.07.2007---23:13:18---rest---id=123456 ----9---a---28.07.2007---23:13:46---rest---id=345677 ----0---a---28.07.2007---23:13:57---rest---id=876543

    Output:

    ---10---a---29.07.2007---11:36:05---rest---id=345677