cunningrat has asked for the wisdom of the Perl Monks concerning the following question:
I've got a batch of code that *works*, that I'm trying to optimize. It reads data from a CSV file, extracts the bits that I'm interested in, and stuffs those bits in a comma-separated format into a hash of arrays.
Code snippet is included below. Fair warning, it's likely to make a Perl guru's ears bleed. I'm an intermediate Perl user, I only pretend to be a guru at work. :p
my @diskline=`grep DISKBUSY $nmondir/$files_by_date{$date} | grep -v B +usy`; foreach my $line (@diskline) { chomp ($line); my @diskarray=split /,/, $line; shift @diskarray; shift @diskarray; for my $i (0..$#diskarray) { $disks_by_date{$date}[$i]=join ',', $disks_by_date{$date}[$ +i],$diskarray[$i] } } my @memline=`grep ^MEM, $nmondir/$files_by_date{$date} | grep -v Memo +ry`; foreach my $line (@memline) { chomp ($line); my @memarray=split /,/, $line; my $rused=100-$memarray[2]; my $mused=100-$memarray[3]; $mem_by_date{$date}[0]=join ',', $mem_by_date{$date}[0],$rused; $mem_by_date{$date}[1]=join ',', $mem_by_date{$date}[1],$mused; } my @pageline=`grep ^PAGE, $nmondir/$files_by_date{$date} | grep -v Pa +ging`; foreach my $line (@pageline) { chomp ($line); my @memarray=split /,/, $line; my $fsin=$memarray[3]-$memarray[5]; my $fsout=$memarray[4]-$memarray[6]; my $recrate= ($memarray[7] == 0) ? 0 : $memarray[8]/$memarray[7] +; $mem_by_date{$date}[2]=join ',', $mem_by_date{$date}[2],$fsin; $mem_by_date{$date}[3]=join ',', $mem_by_date{$date}[3],$fsout; $mem_by_date{$date}[4]=join ',', $mem_by_date{$date}[4],$recrate +; } my $numinterfaces=int(($#{$net_by_date{$date}}+1)/7); my $numl1=$numinterfaces*2; my $numl2=$numinterfaces*4; my @dataline1=`grep ^NET, $nmondir/$files_by_date{$date} | grep -v + Network`; foreach my $line (@dataline1) { chomp ($line); my @netarray=split /,/, $line; shift @netarray; shift @netarray; for my $i (0..$#netarray) { $net_by_date{$date}[$i]=join ',',$net_by_date{$date[$i],$netarra +y[$i]; } } my @dataline2=`grep NETPACKET $nmondir/$files_by_date{$date} | grep + -v Network`; foreach my $line (@dataline2) { chomp ($line); my @netarray=split /,/, $line; shift @netarray; shift @netarray; for my $i (0..$#netarray) { $net_by_date{$date}[$i+$numl1]=join ',',$net_by_date{$date}[$i+$ +numl1],$netarray[$i]; } } my @dataline3=`grep NETERROR $nmondir/$files_by_date{$date} | grep + -v Network`; foreach my $line (@dataline3) { chomp ($line); my @netarray=split /,/, $line; shift @netarray; shift @netarray; for my $i (0..$#netarray) { $net_by_date{$date}[$i+$numl2]=join ',',$net_by_date{$date}[$i+$ +numl2],$netarray[$i]; } }
Here's the problem. I keep thinking that there's *got* to be a way to extract most of this mess into a nice neat subroutine, but the different logic needed to handle different lines keeps tripping me up. I.e. I'm interested in everything for @diskline, fields 2 and 3 from @memline, fields 3-7 from @pageline (which get stuffed into the same array as data from @memline)... etcetera.
Any suggestions would be greatly appreciated...
(Yes, I know I'm trying to parse NMon output. Yes, I know about Nmon Analyzer. No, it won't work for me in this situation.)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Extracting code into a subroutine
by tobyink (Canon) on Oct 30, 2012 at 21:54 UTC | |
Re: Extracting code into a subroutine
by space_monk (Chaplain) on Oct 31, 2012 at 06:49 UTC | |
Re: Extracting code into a subroutine
by grizzley (Chaplain) on Oct 31, 2012 at 09:59 UTC | |
by cunningrat (Acolyte) on Oct 31, 2012 at 13:27 UTC |