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

全部博文(253)

文章存档

2016年(4)

2013年(3)

2012年(32)

2011年(184)

2010年(30)

分类: Python/Ruby

2011-10-10 10:28:27

print output the containt to STDIO.

  1. @fed = qw /test this that/;
  2. print @fed;
  3. print "@fred";

  4. testthisthat
  5. test this that
if the argument is a statement use the parentheses.
print ((2+3)*4);

the return value of print can be true or false. only when you print some I/O error it will be false.

printf format the print.
Generally, you won’t use an array as an argument to printf. That’s because an array
may hold any number of items, and a given format string will work with only a certain
fixed number of items: if there are three conversions in the format, there must be exactly
three items.
  1. my @items = qw( wilma dino pebbles );
  2. printf "The items are:\n".("%10s\n" x @items), @items;

The items are:
     wilma
      dino
   pebbles


  1. print "What column width would you like? ";
  2. chomp(my $width = <STDIN>);
  3. print "Enter some lines, then press Ctrl-D:\n"; # or Ctrl-Z

  4. chomp(my @lines = <STDIN>);
  5. print "1234567890" x (($width+9)/10), "\n"; # ruler line as needed
  6. foreach (@lines) {
  7.   printf "%${width}s\n", $_;
  8. }
%${width}s:the {}is used to insulate the variable width with 's'.

People who have seen printf before may have thought of another solution. Because
printf comes to us from C, which doesn’t have string interpolation, we can use
the same trick that C programmers use. If an asterisk (*) appears in place of a
numeric field width in a conversion, a value from the list of parameters will be used:
printf "%*s\n", $width, $_;




阅读(674) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~