Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Extracting code into a subroutine

by tobyink (Canon)
on Oct 30, 2012 at 21:54 UTC ( [id://1001563]=note: print w/replies, xml ) Need Help??


in reply to Extracting code into a subroutine

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'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1001563]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-20 04:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found