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'