2014-11-24 35 views
0

: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. 

这些错误只是神秘莫测。

+1

将'%gap_ids'的声明移至'while'循环之前。在这种情况下,您不必使用'我们',您可以使用'my'。 – chilemagic 2014-11-25 00:05:11

回答

1

是没有任何理由,你必须使用全局变量声明

您的问题似乎是宣告第一while循环之外%gap_ids解决的。这将哈希将在第二个访问while循环

所以你的代码应该是:

#!/usr/bin/perl 
use strict ; 
use warnings ; 
open (my $fh_tmp, '<', "/tmp/gap_output") ; 

my %gap_ids; 

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 ; 
    my ($key, $value) = ($host_slice, $time_slice) ; 

    #print "$key , $value\n" ; 
    $gap_ids{$key} = $value ; 
} 

while(my ($key, $value) = each %gap_ids) { 
    printf ("%-40s %-6s", $key, $value) ; 
    for (my $index =1; $index <= $value; $index++) { 
     print "#" ; 
    } 
print "\n" ; 
} 

请注意,我有换挡%gap_ids出来。并在第一个循环中更改我们的($ key,$ value)。

1

my声明了一个变量,该变量对于它所在的花括号是局部的。

use strict; 

my $visible_to_the_whole_file = 7; 

{ 
    my $only_visible_in_here = 21; 
} 

print "$visible_to_whole_file\n"; # works ok 

print "$only_visible_in_here\n"; # WILL FAIL, there is no variable any more. 

你可以因为你不使用任何地方package忽略our完全,但它也作用范围大括号。一对花括号称为“块”。

鉴于这种情况,你可以看到为什么你需要声明变量,包括%gap_ids的while循环的两个,如果你想通过代码可以看到里面的while循环的两个值。 (假设use strict

{ 
    my $some_variable = 21; 
} 
{ 
    print $some_variable; # will FAIL, is no longer visible 
} 

这是你想,如果你想两个块里面的变量可见做什么。

my $some_variable; 
{ 
    $some_variable = 21; 
} 
{ 
    print $some_variable; # yay, it's still 21! 
} 

更重要的是,如果你my内两个不同块的变量,你结束了两个不同的变量。

{ 
    my $foo = 21; 
} 
{ 
    my $foo; 
    print "foo is $foo\n"; # won't print 21, it's not 21 any more 
} 

希望对您有所帮助!