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
OUTPUT:
PCName: Foo1
Command1:dfie
PCName: Foo1
Command2:dfo
PCName: Foo1
Command3:dfum
PCName: Foo2
Command1:dfie
PCName: Foo2
Command2:dfo
PCName: Foo2
Command3:dfum
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|