It seems you could factor out some repetition, yes. Given this...

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],$d +iskarray[$i] } } my @memline=`grep ^MEM, $nmondir/$files_by_date{$date} | grep -v Memor +y`; 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; }

You could factor out the execution of the command, looping through lines, chomping, splitting and shifting off fields like this...

sub process (&$;$) { my ($code, $cmd, $skipfields) = @_; my @lines = `$cmd`; for (@lines) { chomp; my @array = split /,/; if ($skipfields) { shift @array for 1..$skipfields; } local $_ = \@array; $code->(); } } process { for my $i (0 .. $#$_) { $disks_by_date{$date}[$i] = join( q[,], $disks_by_date{$date}[$i], $_->[$i], ); } } q{grep DISKBUSY $nmondir/$files_by_date{$date} | grep -v Busy}, 2; process { for my $i (0 .. 1) { my $used = 100 - $_->[2 + $i]; $mem_by_date{$date}[$i] = join( q[,], $mem_by_date{$date}[$i], $used ); } } q{grep ^MEM, $nmondir/$files_by_date{$date} | grep -v Memory};

Actually, maybe we can go further and factor out those joins...

sub joiny { my ($ref, $thing) = @_; $$ref = join q[,], $$ref, $thing; } sub process (&$;$) { my ($code, $cmd, $skipfields) = @_; my @lines = `$cmd`; for (@lines) { chomp; my @array = split /,/; if ($skipfields) { shift @array for 1..$skipfields; } local $_ = \@array; $code->(); } } process { for my $i (0 .. $#$_) { joiny \($disks_by_date{$date}[$i]), $_->[$i]; } } q{grep DISKBUSY $nmondir/$files_by_date{$date} | grep -v Busy}, 2; process { for my $i (0 .. 1) { my $used = 100 - $_->[2 + $i]; joiny \($mem_by_date{$date}[$i]), $used; } } q{grep ^MEM, $nmondir/$files_by_date{$date} | grep -v Memory};

None of this is tested, as I don't have your source files to test against.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

In reply to Re: Extracting code into a subroutine by tobyink
in thread 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.