【问题标题】:Parsing a file into an array将文件解析为数组
【发布时间】:2011-02-04 00:11:40
【问题描述】:

我对 Perl 很陌生,但我基本上想读入一个文件以从中获取一些数据。我想将这些数据解析成一个数组。为什么是数组?因为,我想用数据生成图表(数据的条形图或饼图)。

到目前为止,这是我的 Perl 代码:

#!/usr/bin/perl -w
use warnings;

#Creating an array 
my @cpu_util;

#Creating a test file to read data from
my $file = "test.txt";      
#Opening the while      
open(DATA, $file) || die "Can't open $file: $!\n";

#Looping through the end of the file
while (<DATA>) 
{
if (/(\w+)\s+\d+\s+(\d+)\.(\d+)\%/){ #Getting only the "processes"
chomp;
push @cpu_util, split /\t/; #I was hoping this would split the data at the tabs
}
}

close ($file);

foreach $value (@cpu_util) {
print $value . "\n";
}

这是我正在阅读的文件(test.txt):

=========================================

CPU Utilization 

=========================================

Name      CPU Time     CPU Usage    

-----------------------------------------

System                7962         3.00% 

Adobe Photoshop       6783         0.19% 

MSN Messenger         4490         0.01% 

Google Chrome         8783         0.02% 

Idle                   120        94.00% 

=========================================

但是,我注意到我成功填充了数组,但它没有拆分选项卡并给我一个多维数组。我并不真正关心 CPU 时间字段,但我确实想要 CPU 使用率,所以我想打印一个 XY 图表,其中 Y 轴具有 CPU 使用率和 x 轴,进程名称。

我希望有一个数组“cpu_util”,并且 cpu_util[0][0] = System 和 cpu_util[0][1] = 3.00。这甚至可能吗?我以为拆分 /\/t\ 会解决它,但显然我错了......

【问题讨论】:

    标签: perl


    【解决方案1】:

    我想你想要一个哈希。键是 CPU 名称,值是使用百分比。您快到了。成功匹配后,使用捕获($1$2)填充哈希:

    my %cpu_util;   
    while (<DATA>) 
        {
        if (/(.*?)\s+\d+\s+(\d+\.\d+)\%/){ #Getting only the "processes"
            $cpu_util{ $1 } = $2;
            }
        }
    
    use Data::Dumper;   
    print Dumper( \%cpu_util );
    

    你的数据结构应该更容易使用:

    $VAR1 = {
              'Google Chrome' => '0.02',
              'System' => '3.00',
              'Adobe Photoshop' => '0.19',
              'Idle' => '94.00',
              'MSN Messenger' => '0.01'
            };
    

    您的split 可能不起作用,因为那里没有标签。无论如何,这不是真正要走的路。

    【讨论】:

    • 谢谢布赖恩,成功了。我现在要尝试查找,如何绘制这些数据。我确实研究了 Perl 的 1 美元和 2 美元的使用情况,但这对我来说仍然没有多大意义。你能不能给我指出一个解释得很好的地方?我不明白到底发生了什么:>$cpu_util{ $1 } = $2;
    【解决方案2】:

    这是你的代码修复 ihateme:

    use Data::Dumper;
    #use strict;
    
    #Creating an array
    my @cpu_util;
    
    #Creating a test file to read data from
    my $file = "bar.txt";
    #Opening the while
    open(DATA, $file) || die "Can't open $file: $!\n";
    
    #Looping through the end of the file
    while (<DATA>)
    {
      if (/(\w+)\s+\d+\s+(\d+)\.(\d+)\%/)
      {
        chomp;
        my ( $name, $cpuusage, @rest  ) = split ( /\s+\d+\s+(\d+\.\d+)\%/);
        push @cpu_util, [$name, $cpuusage ];
      }
    
    }
    close $file;
    
    print Dumper(\@cpu_util);
    $ref = $cpu_util[0];  # this will set the reference to the first array
    $ref -> [2]; # this will return the 3rd value
    print Dumper ($ref -> [1]);
    
    
     $VAR1 = [
          [
            'System',
            '3.00'
          ],
          [
            'Adobe Photoshop',
            '0.19'
          ],
          [
            'MSN Messenger',
            '0.01'
          ],
          [
            'Google Chrome',
            '0.02'
          ],
          [
            'Idle',
            '94.00'
          ]
        ];
    

    $VAR1 = '3.00';

    【讨论】:

    • 你在那里做的工作有点太多了。你真的只需要一场比赛。在第一场比赛中抓住你需要的东西,这样你就不必再做第二场比赛了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多