Okay, monks.

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.)


In reply to Extracting code into a subroutine by cunningrat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.