是否可以以 LIFO 或 FIFO 方式使用散列?如何确保哈希以与添加元素相同的顺序打印?
#!/usr/bin/perl
print "content-type: text/html \n\n";
# BEGINNING HASH
%coins = ( "Quarter" , .25,
"Dime" , .10,
"Nickel", .05 );
# PRINT THE OLD HASH
while (($key, $value) = each(%coins)){
print $key.", ".$value."<br />";
}
# ADD NEW ELEMENT PAIRS
$coins{Penny} = .01;
$coins{HalfDollar} = .50;
# PRINT THE NEW HASH
print "<br />";
while (($key, $value) = each(%coins)){
print $key.", ".$value."<br />";
}
这给
镍,0.05 一角钱,0.1 季度,0.25 镍,0.05 一角钱,0.1 半美元,0.5 一分钱,0.01 季度,0.25