第十三章:字符串与排序

上一篇 / 下一篇  2008-11-12 11:40:21 / 个人分类:《Perl语言入门》-小骆驼书-练习题答案

1)
use warnings;
my @numbers = qw/17 1000 04 1.50 3.14159 -10 1.5 4 2001 90210 666/;
my @sortByNum = sort { $b <=> $a } @numbers;
foreach (@sortByNum) {
 printf "%10g\n", $_;
}

2)
use warnings;
my %last_name = qw{
 fred flintstone Wilma Flintstone Barney Rubble
 betty rubble Bamm-Bamm Rubble PEBBLES FLINTSTONE 
};
sub by_case_insensitive{
 "\L$last_name{$a}" cmp "\L$last_name{$b}"
  or
 "\L$a" cmp "\L$b";
}
my @result = sort by_case_insensitive keys %last_name;
foreach (@result){
 print "$last_name{$_}, $_\n";
}

3)
use warnings;
print "Please enter a string: ";
chomp(my $str = <STDIN>);
print "Please enter a substring: ";
chomp(my $sub_str = <STDIN>);
my $position = -1;
while(1){
 $position = index($str, $sub_str, $position+1);
 unless ($position == -1){
  print "$position ";
 } else{
  last; 
 }
}


TAG:

 

评分:0

我来说两句

日历

« 2024-04-30  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 47854
  • 日志数: 80
  • 建立时间: 2008-10-27
  • 更新时间: 2009-07-17

RSS订阅

Open Toolbar