我可能倾向于写这样的东西:
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw( sum );
my @data;
while (my $line = <DATA>) {
my ($state, $voltage, $current_unit, $obs) = split ' ', $line, 4;
my @obs = ($obs =~ / ( [0-9] [.] [0-9]+ )/gx);
push @data, {
state => $state,
voltage => $voltage,
current_unit => $current_unit,
obs => \@obs,
average_current => sum(@obs) / @obs,
};
}
for my $x (@data) {
printf(
"State = %-16sAverage current = %.3f%s\n",
@$x{qw(state average_current current_unit)},
);
}
__DATA__
ICC2_DPD 2.7V ma 0.006 0.006 0.006
DPD_Rel 2.7V ma 0.062 0.054 0.040 0.065 0.037 0.066 0.071 0.073
输出:
状态 = ICC2_DPD 平均电流 = 0.006ma
状态 = DPD_Rel 平均电流 = 0.059ma