Hi
I have taken another approach to what I think you are trying to do. Hopefully you can use this to achieve what you want.
use strict;
use warnings;
my @tmp;
my @keep;
my $first_time = 1;
while (my $a_line = <DATA>) {
chomp $a_line;
push @tmp, $a_line;
if ( $a_line =~ /^\d+\s+\D+/ )
{
if ($first_time)
{
$first_time = 0;
}
else
{
#we have a new host master record
#process the data from the previous host
process_previous(\@tmp);
@keep = (@keep, @tmp);
@tmp =();
}
}
}
process_previous(\@tmp);
@keep = (@keep, @tmp);
@tmp=();
print "$_\n" for @keep;
exit(0);
#----------------- SUBS ----------------------------
sub process_previous {
my $array_ref = shift;
my $keep_this_data = 0;
foreach my $elem(@$array_ref)
{
if ($elem !~ /\d:\d:\d$/ )
{
$keep_this_data = 1;
last;
}
}
if (!$keep_this_data)
{
#empty the array
@$array_ref = ();
}
}
__DATA__
7 hostname12 Generic-legacy 10000000AB210ACF6 ---
10000000AB210ACF4 2:5:4
10000000AB210ACF4 2:3:4
10000000AB210ACF6 3:5:4
9 hostname13 Generic 10000000AB2A3006A 3:5:2
10000000AB2A30068 2:5:2
23 srvernam Generic-legacy 5001438002A3004A 3:3:3
5001438002A3004A ---
5001438002A30048 2:3:3
5001438002A30048 2:5:2
5001438002A30048 2:5:2
9 hostname13 Generic 10000000AB2A3006A 3:5:2
10000000AB2A30068 2:5:2
21 HOSTNAME Generic
9 hostname13 Generic 10000000AB2A3006A 3:5:2
10000000AB2A30068 2:5:2
output
7 hostname12 Generic-legacy 10000000AB210ACF6 ---
10000000AB210ACF4 2:5:4
10000000AB210ACF4 2:3:4
10000000AB210ACF6 3:5:4
23 srvernam Generic-legacy 5001438002A3004A 3:3:3
5001438002A3004A ---
5001438002A30048 2:3:3
5001438002A30048 2:5:2
5001438002A30048 2:5:2
21 HOSTNAME Generic
I hope this helps.
Arnaud
-
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.
|