法1:

#!/usr/bin/perl -w

use strict;

my %hash = (a => 3, b => 1, c => 4);

map{ print \"$_ => $hash{$_}\\n\"}

    sort{ $hash{$b} <=> $hash{$a} } keys %hash;

法2:

 

Perl由于有了引用,使得我们可以在不同的数据结构之间灵活的嵌套数据结构。
比方说,Hash的value可以是标量,也可以嵌套list,甚至还可以继续嵌套hash。

这样使得我们写代码的确方便了不少,但有的时候我们希望对这些复杂的数据结构
有个直观的感受,也就是说能够用perl的语法吧数据结构以及实际值表示出来。这在开发阶段尤其
有用!

正好Perl的模块Data::Dumper可以帮助我们干这件事。

Data::Dumper有面向对象和直接使用函数两种调用方法,

这里介绍直接使用函数的方式,简单好用,应该能够满足绝大多数需求:

Dumper接收的参数为一个标量的列表或者一个引用的列表。

use Data::Dumper;


my $a = \"good\";
my $b = \"bad\";
my @my_array = (\"hello\", \"world\", \"123\", 4.5);
my %some_hash = (\"foo\", 35, \"bar\", 12.4, 2.5, \"hello\",
          \"wilma\", 1.72e30, \"betty\", \"bye\\n\");

##使用函数
print Dumper($a);
print Dumper(\\@my_array);
print Dumper(\\%some_hash);
print Dumper((\\%some_hash, \\@my_array));

运行效果:
perl dump.pl
$VAR1 = \'good\';
$VAR1 = [
          \'hello\',
          \'world\',
          \'123\',
          \'4.5\'
        ];
$VAR1 = {
          \'betty\' => \'bye
\',
          \'bar\' => \'12.4\',
          \'wilma\' => \'1.72e+30\',
          \'foo\' => 35,
          \'2.5\' => \'hello\'
        };
$VAR1 = {
          \'betty\' => \'bye
\',
          \'bar\' => \'12.4\',
          \'wilma\' => \'1.72e+30\',
          \'foo\' => 35,
          \'2.5\' => \'hello\'
        };
$VAR2 = [
          \'hello\',
          \'world\',
          \'123\',
          \'4.5\'
        ];
程序的输出会按照引用在list中的位置自动命名VAR[n].


Data::Dumper模块使用小例


Data::Dumper模块主要用途是:给出一个或多个变量,包括引用,以PERL语法的方式返回这个变量的内容。
比方说,这里有个很复杂的hash,数据结构很复杂,我想看看这个hash里面的内容。除了常见的方式(直接
用print或者编历keys然后打印), 我们也可以使用Data::Daumper->Dump([\\%hash])的形式。同时,
模块中定义了很多的配置参数,让用户可以调整打印格式。

简单列举几个(具体参见perldoc),这些变量在模块
$Data::Dumper::Indent 
这个设置打印的缩进格式,可以设置成0,1,2和3。用户可以自己尝试下。
$Data::Dumper::Terse
如果设置这个变量,则不打印变量的名字,只打印变量的内容。
$Data::Dumper::Maxdepth
不超过这个变量的限制深度,才打印变量的内容。

下面写个程序说明问题:

#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

my%people=(
    \'name\'=>\'ray\',
    \'age\'=> 24,
    \'sex\'=>\'man\',
    \'food\'=>[\'egg\',\'apple\'],
);



# See Data::Dumper module to get the default vaule of the 
# following module gobal variable. You can overwrite the
# default value to user defined one.

print\"Show perl hash, with pre-defined variable name\\n \";
print\"and without maxdepth\\n\";
$Data::Dumper::Terse = 0;# default is 0
$Data::Dumper::Indent = 3;# default is 2
$Data::Dumper::Maxdepth = 0;# default is 0
my $variable_name=\'*\'.\"my_info\";
print Data::Dumper->Dump([\\%people],[$variable_name]);

print\"Show perl hash, without pre-defined variable name\\n \";
print\"and with maxdepth is 1\\n\";
$Data::Dumper::Terse = 1;# default is 0
$Data::Dumper::Indent = 2;# default is 2
$Data::Dumper::Maxdepth = 1;# default is 0
$variable_name=\'$\'.\"my_info\";
print Data::Dumper->Dump([\\%people],[$variable_name])

 

ray@localhost perl]$ perl data_dumper.pl
Show perl hash, with pre-defined variable name
 and without maxdepth
%my_info = (
             \'food\' => [
                         #0
                         \'egg\',
                         #1
                         \'apple\'
                       ],
             \'name\' => \'ray\',
             \'sex\' => \'man\',
             \'age\' => 24
           );
Show perl hash, without pre-defined variable name
 and with maxdepth is 1
{
             \'food\' => \'ARRAY(0x91d68c4)\',
             \'name\' => \'ray\',
             \'sex\' => \'man\',
             \'age\' => 24
           }

收藏 打印