Chinaunix首页 | 论坛 | 博客
  • 博客访问: 928457
  • 博文数量: 245
  • 博客积分: 11429
  • 博客等级: 上将
  • 技术积分: 2662
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-15 00:16
文章存档

2011年(56)

2010年(174)

2009年(15)

分类: Python/Ruby

2011-06-27 17:23:35

    1.截取屏幕输入字符串,然后根据ASCII 排序。一个字符串一行。
    #!/usr/bin/perl -w
    print "Please input string,one per line,Crtl-D:\n";
    chomp(@lines = );
    @sorted = sort @lines;
    print "You stdin strings is : \n @lines \n";
    print "The sort is : \n @sorted\n";
    输出:
    You stdin strings is :
     a  a b d c e g f
    The sort is :
     a a  b c d e f g
      2. 自定义数组,然后获取键盘输入的数字,根据数字进行对数组遍历打印。
    1. #!/usr/bin/perl -w #-w 为报警使用
    2. @name = qw (a b c d e);# 定义数组
    3. print "Enter some number from 1-5,one per line,then press Crtl-D:\n";#输入数字时,记着输一个按下回车。
    4. #去掉输入行末尾的换行符
    5. chomp(@numbers = <STDIN>);
    6. #进行排错时使用,将变量转换为直接变量
    7. #$sum = @numbers +1;
    8. #打印直接变量
    9. #print $sum;
    10. #用foreach进行遍历
    11. foreach (@numbers){
    12. #打印$_ 中的列表,$_默认为前面未定义的数组的集合。此处代表@numbers开头的控制变量。
    13. #print "$_";
    14. print "$name[ $_ - 1 ]\n";
    15. }
    阅读(612) | 评论(0) | 转发(0) |
    给主人留下些什么吧!~~