Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl 将文件解析为数组_Perl - Fatal编程技术网

Perl 将文件解析为数组

Perl 将文件解析为数组,perl,Perl,我对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

我对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";
}
然而,我注意到我成功地填充了数组,但它没有分割选项卡并给我一个多维数组。我并不真正关心CPU时间字段,但我确实需要CPU使用率,因此我想打印一个XY图表,其中Y轴包含CPU使用率,x轴是进程名称


我希望有一个数组“cpu_util”,并使cpu_util[0][0]=系统和cpu_util[0][1]=3.00。这可能吗?我原以为拆分会解决这个问题,但显然我弄错了…

我想你需要一个哈希。键是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 );

你的标签可能不起作用,因为那里没有标签。无论如何,这不是一条真正的路。

我想你想要一份杂烩。键是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 );

你的标签可能不起作用,因为那里没有标签。无论如何,这不是一条真正的道路。

这是您的代码修复版ihatem:

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'
      ]
    ];
使用数据::转储程序;
#严格使用;
#创建数组
我的@cpu\u util;
#创建要从中读取数据的测试文件
my$file=“bar.txt”;
#开始时
打开(数据,$file)| |死亡“无法打开$file:$!\n”;
#在文件末尾循环
而()
{
如果(/(\w+)\s+\d+\s+(\d+)\(\d+)\%/)
{
咀嚼;
我的($name,$cpuusage,@rest)=拆分(/\s+\d+\s+(\d+\.\d+\%/);
push@cpu_util,[$name,$cpuusage];
}
}
关闭$file;
打印转储程序(\@cpu\u util);
$ref=$cpu_util[0];#这将设置对第一个数组的引用
$ref->[2];#这将返回第三个值
打印转储程序($ref->[1]);
$VAR1=[
[
"制度",,
'3.00'
],
[
“Adobe Photoshop”,
'0.19'
],
[
“MSN Messenger”,
'0.01'
],
[
“谷歌浏览器”,
'0.02'
],
[
"闲置",,
'94.00'
]
];

$VAR1='3.00'

这是您的代码修复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'
      ]
    ];
使用数据::转储程序;
#严格使用;
#创建数组
我的@cpu\u util;
#创建要从中读取数据的测试文件
my$file=“bar.txt”;
#开始时
打开(数据,$file)| |死亡“无法打开$file:$!\n”;
#在文件末尾循环
而()
{
如果(/(\w+)\s+\d+\s+(\d+)\(\d+)\%/)
{
咀嚼;
我的($name,$cpuusage,@rest)=拆分(/\s+\d+\s+(\d+\.\d+\%/);
push@cpu_util,[$name,$cpuusage];
}
}
关闭$file;
打印转储程序(\@cpu\u util);
$ref=$cpu_util[0];#这将设置对第一个数组的引用
$ref->[2];#这将返回第三个值
打印转储程序($ref->[1]);
$VAR1=[
[
"制度",,
'3.00'
],
[
“Adobe Photoshop”,
'0.19'
],
[
“MSN Messenger”,
'0.01'
],
[
“谷歌浏览器”,
'0.02'
],
[
"闲置",,
'94.00'
]
];

$VAR1='3.00'

谢谢你,布莱恩,这就成功了。我现在将尝试查找,如何绘制这些数据。我确实从Perl的角度研究了$1和$2的使用情况,但对我来说仍然没有多大意义。你能给我指一个解释得很好的地方吗?我不明白到底发生了什么:>$cpu_util{$1}=$2;谢谢你,布莱恩,这就成功了。我现在将尝试查找,如何绘制这些数据。我确实从Perl的角度研究了$1和$2的使用情况,但对我来说仍然没有多大意义。你能给我指一个解释得很好的地方吗?我不明白到底发生了什么:>$cpu_util{$1}=$2;你在那里做的工作有点太多了。你真的只需要一场比赛。在第一场比赛中抢到你需要的东西,这样你就不必再做第二场比赛了。你在那里做的工作有点太多了。你真的只需要一场比赛。在第一场比赛中抓住你需要的东西,这样你就不必再做第二场比赛了。