use List::Util 'sum'; # might be overkill sub cpu_stat { open(STAT_FILE, "< /proc/stat") or warn "/proc/stat: $!"; my $line = ; close(STAT_FILE); $line =~ /^cpu\s+(\d+) (\d+) (\d+) (\d+)/; } my @before = cpu_stat(); sleep 1; my @after = cpu_stat(); my $ticks_past = sum(@after) - sum(@before); my $scale = 100 / $ticks_past if $ticks_past; # divide by zero error # Arguably, this is less readable than your code printf("user: %d%%, nice: %d%%, total: %d%%, idle: %d%%\n", map { ($after[$_] - $before[$_]) * $scale } 0 .. 3); #### my %hash; @hash{qw/ user nice total idle /} = cpu_stat(); print "nice: $hash{nice}\n";