- 论坛徽章:
- 6
|
本帖最后由 stanley_tam 于 2013-10-10 21:38 编辑
- r b swpd free buff cache si so bi bo in cs us sy id wa st
- 2 0 213728 87600 1183808 849440 0 0 86 85 2 0 3 3 92 2 0
- 0 0 213728 88864 1183808 849460 0 0 0 0 1022 372 5 3 93 0 0
- 0 0 213728 88740 1183808 849472 0 0 0 2 1014 285 0 0 100 0 0
- while (local $_ = <PH>) {
- next if /---/;
- s/^\s+|\s+$//g;
- if (/\d+/ && @keys) {
- @update{@keys} = split(/\s+/,$_);
- } else { @keys = split(/\s+/,$_); }
- }
- /\d+/ && @keys => 是指这一行里有数字,且@keys存在
- 如果这一行里没有数字,那肯定就是行:r b swpd free buff cache si so bi bo in cs us sy id wa st
- @keys = split(/\s+/,$_) =>将这一行按空格split,赋值给@keys
- Perl就是随意了一点,就落下了一个write only的计算机语言的评价。
- 建议遵循<<Perl最佳实践>> 一书
- 我的话会这么写,应该会清楚一点。。。 :)
- while (defined(my $line = readline PH)) {
- # skip --- line
- next if $line =~ m{---};
-
- # trim heading or trailing spaces
- $line =~ m{^\s+ | \s+$}mgx;
- # if line contain virtual memory state values
- if ($line =~ m{ \d+ }x and scalar @keys) {
- my @vmstat_numbers = split /\s+/, $line;
- # swallow them to hash %update;
- @update{@keys} = @vmstat_numbers;
- }
- else {
- # vmstat head data, use it as keys for hash %update
- @keys = split /\s+/, $line;
- }
- }
复制代码 回复 8# Iinvincible
|
|