Chi*_*moy 1 perl perl-data-structures
我是Perl的初学者,我试图从"Beginning Perl:Curtis Poe"运行这个示例
#!/perl/bin/perl
use strict;
use warnings;
use diagnostics;
my $hero = 'Ovid';
my $fool = $hero;
print "$hero isn't that much of a hero. $fool is a fool.\n";
$hero = 'anybody else';
print "$hero is probably more of a hero than $fool.\n";
my %snacks = (
stinky => 'limburger',
yummy => 'brie',
surprise => 'soap slices',
);
my @cheese_tray = values %snacks;
print "Our cheese tray will have: ";
for my $cheese (@cheese_tray) {
print "'$cheese' ";
}
print "\n";
Run Code Online (Sandbox Code Playgroud)
当我在带有ActivePerl的windows7系统和codepad.org上试用时,输出上面的代码
Ovid isn't that much of a hero. Ovid is a fool.
anybody else is probably more of a hero than Ovid.
Our cheese tray will have: 'limburger''soap slices''brie'
Run Code Online (Sandbox Code Playgroud)
我不清楚第三行是打印'limburger''soap slices''brie'但哈希顺序是'limburger''brie''soap slices'.
请帮我理解.
哈希不订购.如果需要特定订单,则需要使用数组.
例如:
my @desc = qw(stinky yummy surprise);
my @type = ("limburger", "brie", "soap slices");
my %snacks;
@snacks{@desc} = @type;
Run Code Online (Sandbox Code Playgroud)
现在你有了类型@type.
你当然也可以使用sort:
my @type = sort keys %snacks;
Run Code Online (Sandbox Code Playgroud)