:Hello Everybody。 我想解析这个数据到一个直方图。 当我解析文件和子串出我的价值观和漂亮的小数据集。perl在填充它的范围之外引用散列(直方图)
#!/usr/bin/perl
use strict ;
use warnings ;
open (my $fh_tmp, '<', "/tmp/gap_output") ;
while(<$fh_tmp>) {
my ($date, $time, $host) = split ;
my $host_length = 12;
my $time_length = 5;
my $host_slice = substr $host, 0 , $host_length ;
my $time_slice = substr $time, 0 , $time_length ;
print "$host_slice, $time_slice\n" ;
}
请参阅 - 这太棒了。
trip7829host, 03:10
trip7829host, 03:10
trip7829host, 03:10
trip7829host, 03:10
trip7829host, 03:10
trip7829host, 03:10
trip7829host, 03:10
trip7829host, 03:10
trip7829host, 07:10
trip7829host, 07:10
trip7829host, 07:10
trip7829host, 07:10
trip7829host, 07:10
trip7829host, 07:10
trip7829host, 07:10
trip7829host, 07:10
trip7829host, 07:10
trip7829host, 07:10
trip7829host, 03:10
trip7829host, 03:10
trip7830host, 07:30
trip7830host, 07:30
trip7831host, 07:30
trip7831host, 07:30
trip7832host, 07:30
最终我需要得到的是这样的东西。 uniqe主机,唯一的时间和每分钟发生此错误的次数(未显示)。 现在发生在我身上 - 我可能需要一个多级哈希(ugg)。 HOwever,直到这个问题 - 我甚至不能填充一个正常的散列。
trip7829host, 03:10 10 ##########
trip7829host, 07:10 10 ##########
trip7830host, 07:30 2 ##
trip7831host, 07:30 2 ##
trip7832host, 07:30 1 #
我试着用一堆我的和我们的。虽然并不重要。 我有点认为,一旦gap_ids被填充,我不会有 再次声明它 - 它会住在while循环的范围之外 - 但它不,因为使用严格要我再次声明它。我试图用'我们'导入 这些值。
[email protected]:~/walt/historgram$ cat gap_histgram
#!/usr/bin/perl
use strict ;
use warnings ;
open (my $fh_tmp, '<', "/tmp/gap_output") ;
while(<$fh_tmp>) {
my ($date, $time, $host) = split ;
our %gap_ids ;
my $host_length = 12;
my $time_length = 5;
my $host_slice = substr $host, 0 , $host_length ;
my $time_slice = substr $time, 0 , $time_length ;
our ($key, $value) = ($host_slice, $time_slice) ;
#print "$key , $value\n" ;
$gap_ids{$key} = $value ;
}
while(($key, $value) =each %gap_ids) {
printf ("%-40s %-6s", $key, $value) ;
for (my $index =1; $index <= $value; $index++) {
print "#" ;
}
print "\n" ;
}
-rwxr-xr-x 1 casper casper 643 Nov 24 16:03 gap_histgram*
[email protected]:~/walt/historgram$ ./gap_histgram
Variable "$key" is not imported at ./gap_histgram line 18.
Variable "$value" is not imported at ./gap_histgram line 18.
Variable "%gap_ids" is not imported at ./gap_histgram line 18.
Variable "$key" is not imported at ./gap_histgram line 19.
Variable "$value" is not imported at ./gap_histgram line 19.
Variable "$value" is not imported at ./gap_histgram line 20.
Global symbol "$key" requires explicit package name at ./gap_histgram line 18.
Global symbol "$value" requires explicit package name at ./gap_histgram line 18.
Global symbol "%gap_ids" requires explicit package name at ./gap_histgram line 18.
Global symbol "$key" requires explicit package name at ./gap_histgram line 19.
Global symbol "$value" requires explicit package name at ./gap_histgram line 19.
Global symbol "$value" requires explicit package name at ./gap_histgram line 20.
Execution of ./gap_histgram aborted due to compilation errors.
这些错误只是神秘莫测。
将'%gap_ids'的声明移至'while'循环之前。在这种情况下,您不必使用'我们',您可以使用'my'。 – chilemagic 2014-11-25 00:05:11