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

全部博文(253)

文章存档

2016年(4)

2013年(3)

2012年(32)

2011年(184)

2010年(30)

分类: Python/Ruby

2011-07-25 10:57:56

。。。

  1. #!/usr/bin/perl

  2. my @names = qw /fred barney betty dino Wilma pebbles/;
  3. my $result = &which_element_is("dino", @names);

  4. print "index is $result\n";

  5. sub which_element_is{
  6.     my($what, @array) = @_;
  7.     foreach(0 .. $#array){
  8.         if($what eq $array[$_]){
  9.             return $_;
  10.         }
  11.     }
  12.     -1;
  13. }
组@array(第一个索引值为0,最后一个为$#array

非标量返回值
sub list_from_fred_to_barney{
    $a = shift;
    $b = shift;
    if($a < $b){
        $a .. $b;
    }else{
        reverse $b .. $a;
    }
}

@c = &list_from_fred_to_barney(10,4);
print "@c\n";

Since all Perl subroutines can be called in a way that needs a return value, it’d be a bit
wasteful to have to declare special syntax to “return” a particular value for the majority
of the cases. So Larry made it simple. As Perl is chugging along in a subroutine, it is
calculating values as part of its series of actions. Whatever calculation is last performed in a subroutine is automatically also the return value.
  1. sub func{
  2.     1 + 2;
  3. }

  4. print func;

  5. we get 3;

  1. sub func{
  2.     1 + 2;
  3.     if (2 < 1){
  4.         1;
  5.     }else{
  6.         3;}
  7. }

  8. print func;
  9. get 3.

  1. sub sum_of_fred_and_barney {
  2.   print "Hey, you called the sum_of_fred_and_barney subroutine!\n";
  3.   $fred + $barney; # That's not really the return value!
  4.   print "Hey, I'm returning a value now!\n
In this example, the last expression evaluated is not the addition; it’s the print state-
ment. Its return value will normally be 1, meaning “printing was successful,”* but that’s
not the return value you actually wanted. So be careful when adding additional code
to a subroutine, since the last expression evaluated will be the return value.

So, what happened to the sum of $fred and $barney in that second (faulty) subroutine?
We didn’t put it anywhere, so Perl discarded it. If you had requested warnings, Perl
(noticing that there’s nothing useful about adding two variables and discarding the
result) would likely warn you about something like “a useless use of addition in a void context.” The term void context is just a fancy way of saying that the answer isn’t being
stored in a variable or used in any other way.

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

  4. my @names = qw/ fred barney betty dino wilma pebbles bamm-bamm /;
  5. my $r = &find_index("barney", @names);
  6. print $r;
  7. sub find_index{
  8.     my ($des, @array) = @_;

  9.     foreach (0..$#array){
  10.         if ($array[$_] eq $des){
  11.             return $_;
  12.         }
  13.     }
  14.     -1;
  15. }

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

  4. my @fred = qw{ 1 3 5 7 9 };
  5. my $fred_total = total(@fred);
  6. print "The total of \@fred is $fred_total.\n";
  7. print "Enter some numbers on separate lines: ";
  8. my $user_total = total(<STDIN>);
  9. print "The total of those numbers is $user_total.\n";



  10. sub total{
  11.     my $sum;

  12.     foreach (@_){
  13.         $sum += $_;
  14.     }
  15.     $sum;
  16. }




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

上一篇:子程序--parameters

下一篇:输入

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