Kenton Brede wrote:
> I've cobbled some code together that will allow me to parse a file
> snarfing 4 lines which consist of server name and Daily output of
> bandwith usage. I've pasted an example of what I have at the bottom of
> this mail. If anyone would like to take the time to show me how it
> should really be done I would apreciate it. I'm not exactly a
> programming wonder and trying to learn.
> Thanks,
> Kent
>
> #!/usr/bin/perl
> use warnings;
> use strict;
good practice.
> my @lines;
>
> while ( <DATA> ) {
> push ( @lines, $_ );
> }
>
do you really need to store all the lines in an array?
> for ( my $count = 0; $count <= $#lines; $count++ ) {
> if ( $lines[$count] =~ m/\*/g ) { # grab server names
> print "$lines[$count]";
> }
> if ( $lines[$count] =~ m/Daily/g ) { # grab daily output
> print "$lines[$count] $lines[$count+1] $lines[$count+2]\n";
> }
> }
this is very c-ish which is fine for your purpose but Perl encourages a
different style. if you just want to print out the server and the daily
average, there are many ways to accomplish that. one way of doing it would
be:
#!/usr/bin/perl -w
use strict;
my $found;
while(<DATA>){
$found = 1 && print if /^\*/;
if($found){
$found = 0;
while(<DATA>){/Daily/ && last}
print <DATA> . <DATA>;
}
}
__DATA__
* Leo
# Bandwidth Usage
eth0 Total for 106.95 days:
RX 307.28 MB
TX 768.05 MB
eth0 Daily average:
RX 2.87 MB
TX 7.18 MB
* Buffy2
# Bandwidth Usage
eth0 Total for 14.70 days:
RX 141.28 MB
TX 2.03 MB
eth0 Daily average:
RX 9.61 MB
TX 0.14 MB
__END__
prints:
* Leo
RX 2.87 MB
TX 7.18 MB
* Buffy2
RX 9.61 MB
TX 0.14 MB
i think the only line that deserve any explanation is:
print <DATA> . <DATA>;
which simply forecs <DATA> to be in scalar context so it return the next 2
lines which we know will be the stat of the server.
david
--
s,.*,<<,e,y,\n,,d,y,.s,10,,s
.ss.s.s...s.s....ss.....s.ss
s.sssss.sssss...s...s..s....
...s.ss..s.sss..ss.s....ss.s
s.sssss.s.ssss..ss.s....ss.s
..s..sss.sssss.ss.sss..ssss.
..sss....s.s....ss.s....ss.s
,....{4},"|?{*=}_'y!'+0!$&;"
,ge,y,!#:$_(-*[./<[EMAIL PROTECTED],b-t,
.y...,$~=q~=?,;^_#+?{~,,$~=~
y.!-&*-/:[EMAIL PROTECTED] ().;s,;,
);,g,s,s,$~s,g,y,y,%,,g,eval
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>