Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: File Find/Replace with the replacement coming from part of earlier matched string

by tmharish (Friar)
on Feb 05, 2013 at 06:25 UTC ( #1017069=note: print w/replies, xml ) Need Help??

in reply to File Find/Replace with the replacement coming from part of earlier matched string

RegEx on multiple lines that might be large with look-ahead can really slow things down.

There is no need to hold onto chunks of anything - all you need is the PC name and you are fine up to when you see the next PC name.

The following chunk of code does that:

use strict ; use warnings ; my @log_data = <DATA> ; my $current_pc_name ; foreach my $log_line ( @log_data ) { chomp( $log_line ); next unless( $log_line ) ; my ( $left, $right ) = split( /\:/, $log_line ) ; if( $left eq 'PCName' ) { $current_pc_name = $right ; next ; } unless( $current_pc_name ) { die( "Command $log_line assigned to no PC!!" ) ; } print "PCName:$current_pc_name\n" ; print "$log_line\n\n" ; } __DATA__ PCName: Foo1 Command1:dfie Command2:dfo Command3:dfum PCName: Foo2 Command1:dfie Command2:dfo Command3:dfum


PCName: Foo1 Command1:dfie PCName: Foo1 Command2:dfo PCName: Foo1 Command3:dfum PCName: Foo2 Command1:dfie PCName: Foo2 Command2:dfo PCName: Foo2 Command3:dfum

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1017069]
[Lady_Aleena]: Well, not much of my code. A few little things people here like. The big things though, the more important to me code, people here seem to loathe.
[atcroft]: Lady_Aleena: My early code was pretty horrible... but somewhere early on I learned not to take criticism of my code personally... which was probably a good thing, considering the kind of code I wrote at the time. :\
[Lady_Aleena]: I am hungry, so I'm going to go make something to eat. I hope you figure it out atcroft. Have a nice night!
[atcroft]: I have one script that I have started re-writing several times over the years, and have yet to get it working in a cleaner/better form. (It is some smelly code-I did not even know about hashes at the time I wrote it.)
[atcroft]: Enjoy, Lady_Aleena!

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2017-05-27 05:30 GMT
Find Nodes?
    Voting Booth?