Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

process records in file

Status
Not open for further replies.

hudo

Programmer
Dec 4, 2003
94
DE
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

Code:
###  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";} 
###  ende timestamp##################################
 
do you have any code written yet? Are the '---' field delimiters or something else?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I assume that your use of 4 dashes ---- in the first column is a mistype, and it should instead be 3. Anyway, just use Tie::File.


Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]Fcntl[/green] [red]'[/red][purple]:flock[/purple][red]'[/red][red];[/red]
[black][b]use[/b][/black] [green]POSIX[/green] [red]qw([/red][purple]strftime[/purple][red])[/red][red];[/red]
[black][b]use[/b][/black] [green]Tie::File[/green][red];[/red]

[black][b]use[/b][/black] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$datafile[/blue] = [red]'[/red][purple]file.txt[/purple][red]'[/red][red];[/red]

[black][b]my[/b][/black] [blue]$o[/blue] = [url=http://perldoc.perl.org/functions/tie.html][black][b]tie[/b][/black][/url] [black][b]my[/b][/black] [blue]@array[/blue], [red]'[/red][purple]Tie::File[/purple][red]'[/red], [blue]$datafile[/blue]
	or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Can't open [blue]$datafile[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]
[blue]$o[/blue]->[maroon]flock[/maroon][red]([/red]LOCK_EX[red])[/red][red];[/red]

[olive][b]for[/b][/olive] [black][b]my[/b][/black] [blue]$i[/blue] [red]([/red][fuchsia]0..[/fuchsia][blue]$#array[/blue][red])[/red] [red]{[/red]
	[black][b]my[/b][/black] [blue]$line[/blue] = [blue]$array[/blue][red][[/red][blue]$i[/blue][red]][/red][red];[/red]
	
	[gray][i]# Data Example (showing indexes)[/i][/gray]
	[gray][i]# ---0---m---28.07.2007---23:13:18---rest---id=123456[/i][/gray]
	[gray][i]# 0  1   2   3            4          5      6[/i][/gray]
	
	[black][b]my[/b][/black] [blue]@data[/blue] = [url=http://perldoc.perl.org/functions/split.html][black][b]split[/b][/black][/url] [red]'[/red][purple]---[/purple][red]'[/red], [blue]$line[/blue][red];[/red]
	
	[olive][b]if[/b][/olive] [red]([/red][blue]$data[/blue][red][[/red][fuchsia]6[/fuchsia][red]][/red] eq [red]'[/red][purple]id=345677[/purple][red]'[/red][red])[/red] [red]{[/red]
		[blue]$data[/blue][red][[/red][fuchsia]1[/fuchsia][red]][/red]++[red];[/red]
		
		[black][b]my[/b][/black] [blue]@today[/blue] = [url=http://perldoc.perl.org/functions/localtime.html][black][b]localtime[/b][/black][/url][red];[/red]
		[blue]$data[/blue][red][[/red][fuchsia]3[/fuchsia][red]][/red] = strftime [red]"[/red][purple]%d.%m.%Y[/purple][red]"[/red], [blue]@today[/blue][red];[/red]
		[blue]$data[/blue][red][[/red][fuchsia]4[/fuchsia][red]][/red] = strftime [red]"[/red][purple]%H:%M:%S[/purple][red]"[/red], [blue]@today[/blue][red];[/red]
		
		[blue]$array[/blue][red][[/red][blue]$i[/blue][red]][/red] = [url=http://perldoc.perl.org/functions/join.html][black][b]join[/b][/black][/url] [red]'[/red][purple]---[/purple][red]'[/red], [blue]@data[/blue][red];[/red]
	[red]}[/red]
[red]}[/red]

[gray][i]# Unlock[/i][/gray]
[url=http://perldoc.perl.org/functions/untie.html][black][b]untie[/b][/black][/url] [blue]@array[/blue][red];[/red]
[url=http://perldoc.perl.org/functions/undef.html][black][b]undef[/b][/black][/url] [blue]$o[/blue][red];[/red]

[fuchsia]1[/fuchsia][red];[/red]

[teal]__END__[/teal]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
Core (perl 5.8.8) Modules used :
[ul]
[li]Fcntl - load the C Fcntl.h defines[/li]
[li]POSIX - Perl interface to IEEE Std 1003.1[/li]
[li]Tie::File - Access the lines of a disk file via a Perl array[/li]
[/ul]
[/tt]

Also, as Kevin noted, you're much more likely to get useful assistance if you show the code that you've tried thus far and explain any specific difficulties you're having. However, this should point you in the right directly, if not actually be a working solution.

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top