Chinaunix首页 | 论坛 | 博客
  • 博客访问: 837601
  • 博文数量: 253
  • 博客积分: 6891
  • 博客等级: 准将
  • 技术积分: 2502
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-03 11:01
文章分类

全部博文(253)

文章存档

2016年(4)

2013年(3)

2012年(32)

2011年(184)

2010年(30)

分类: Python/Ruby

2011-10-09 13:39:04

One previously seen operator that returns a different value in an array context is the
line-input operator, . As described earlier, returns the next line of input
in a scalar context. Now, in list context, this operator returns all of the remaining lines
up to the end of file. Each line is returned as a separate element of the list. For example:
@lines = ; # read standard input in list context.
hen the input is coming from a file, this will read the rest of the file. But how can
there be an end-of-file when the input comes from the keyboard? On Unix and similar
systems, including Linux and Mac OS X, you’ll normally type a Control-D† to indicate
to the system that there’s no more input;

  1. #!/usr/bin/perl -w
  2. #
  3. use strict;

  4. print "input the lines and end with Ctrl+D\n";

  5. #open(FILE, 'x_operator.pl') or die "can not open it.";

  6. my @lines = <STDIN>;
  7. #my @lines = <FILE>;
  8. #chomp @lines;

  9. my @newlines = reverse @lines;

  10. print @newlines;

  1. #!/usr/bin/perl -w
  2. #
  3. use strict;

  4. my @name = qw /fred betty barney dino wilma pebbles bamm-bamm/;
  5. my @index = <STDIN>;

  6. chomp @index;

  7. foreach my $i (@index){
  8.     print $name[$i], "\n";
  9. }
能省则省。。。
  1. #!/usr/bin/perl -w
  2. #
  3. use strict;

  4. my @name = qw /fred betty barney dino wilma pebbles bamm-bamm/;
  5. chomp (my @index = <STDIN>);

  6. foreach (@index){
  7.     print $name[$_ - 1], "\n";
  8. }

  1. chomp(@lines = <STDIN>);
  2. @sorted = sort @lines;
  3. print "@sorted\n";



阅读(298) | 评论(0) | 转发(0) |
0

上一篇:lists and arrays

下一篇:$#_

给主人留下些什么吧!~~