Chinaunix首页 | 论坛 | 博客
  • 博客访问: 835453
  • 博文数量: 253
  • 博客积分: 6891
  • 博客等级: 准将
  • 技术积分: 2502
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-03 11:01
文章分类

全部博文(253)

文章存档

2016年(4)

2013年(3)

2012年(32)

2011年(184)

2010年(30)

分类: Python/Ruby

2011-10-24 09:11:53

perl -d test.pl test.txt

x [maxdepth] expr

Evaluates its expression in list context and dumps out the result in a pretty-printed fashion. Nested data structures are printed out recursively, unlike the real function in Perl. When dumping hashes, you'll probably prefer 'x \%h' rather than 'x %h'. See if you'd like to do this yourself.

The output format is governed by multiple options described under .

  1. #!/usr/bin/perl -w
  2. #
  3. #use strict;

  4. my %hash;
  5. my %src;

  6. while(<>){
  7.     next if(/^#/);

  8.     my ($src, $dst, $bytes) = split;
  9.     $hash{$src}{$dst} += $bytes;
  10.     $hash{$src}{"all"} += $bytes;
  11. }
  12. for my $source (sort {$hash{$b}{all} <=> $hash{$a}{all}} keys %hash){
  13. #print "$source => $hash{$source}{$all}\n";
  14.     for my $dest (sort {$hash{$source}{$b} => $hash{$source}{$a}} keys %{$hash{$source}}){
  15.         print " $source => $dest $hash{$source}{$dest}\n";
  16.     }
  17. }
  1. perl -d hash_hash.pl coconet.dat

 DB<1> s

  DB<2> x \%hash
0  HASH(0x9daf80c)
   'gilligan.crew.hut' => HASH(0x9e46294)
      'all' => 4721
      'lovey.howell.hut' => 4721
   'thurston.howell.hut' => HASH(0x9e46214)
      'all' => 4046
      'lovey.howell.hut' => 4046

When we give x a hash reference, it dumps the entire contents of the hash, showing the key/value pairs. If any of the values are also hash references, it dumps those as well, recursively. What we'll see is that the %hash hash has a single key of gilligan.crew.hut, whose corresponding value is another hash reference. The referenced hash contains a two keys of all , with a value of 1250 and lovey.howell.hut value of 4721.






阅读(1154) | 评论(0) | 转发(0) |
0

上一篇:hash in hash

下一篇:Data::Dumper

给主人留下些什么吧!~~