zoukankan      html  css  js  c++  java
  • Perl 学习手札之十一:References and Data Structures

    a reference is a variable that refer to a value

     - as opposed to a variable that contains a value

    example.pl

    #!/usr/bin/perl
    #

    use strict;
    use warnings;

    main(@ARGV);

    sub main
    {
        my $var = "This is the Perl 5 references chapter.";
        my $ref = \$var;
        my $copy = $ref;
        message($$ref);
        $var = 42;
        message($$copy);
    }

    sub message
    {
        my $m = shift or return;
        print("$m\n");
    }

    sub error
    {
        my $e = shift || 'unknown error';
        print("$0: $e\n");
        exit 0;
    }

    variable contains value; reference point to value.


    arrayref.pl

    #!/usr/bin/perl
    #

    use strict;
    use warnings;

    main(@ARGV);

    sub main
    {
        my @list = ("This is the Perl 5 references chapter.",
        42,
        "another string",
        "one more value");
        my $var = \@list;
        message($var);
        my $copy = $var;
        message(join(':',@$var));
        message(join(':',@$copy));
        $copy->[2]=187;
        message(join(':',@$copy));
    }

    sub message
    {
        my $m = shift or return;
        print("$m\n");
    }

    sub error
    {
        my $e = shift || 'unknown error';
        print("$0: $e\n");
        exit 0;
    }

    这个例子包含了所有数组引用的内容:包括输出数组的内容,修改数组的元素值。我们可以将代码的部分写成一下的类型:

        my $var = ["This is the Perl 5 references chapter.",
        42,
        "another string",
        "one more value"];

    这样构造了一个事实上的匿名数组引用, 并且和上面的结果完全等效。

    hashref.pl

    #!/usr/bin/perl
    #

    use strict;
    use warnings;

    main(@ARGV);

    sub main
    {
        my $hashref = {
           "name"=>"jimi hendrix",
           instrument=>"guitar",
           album=>"are you experienced"
        };
        my $copy = $hashref;
        $copy->{songs}= "puplehaze";
        foreach my $k (sort keys %$copy){
            my $v = $copy->{$k};
            message("$k:$v");
        }

    }

    sub message
    {
        my $m = shift or return;
        print("$m\n");
    }

    sub error
    {
        my $e = shift || 'unknown error';
        print("$0: $e\n");
        exit 0;
    }

    同理,上面的例子也是用了所有的关于hash的引用模型和例子。我们还是用的匿名引用。例子同数组部分。

    mixed.pl

    #!/usr/bin/perl
    #

    use strict;
    use warnings;

    main(@ARGV);
        my $artists=[
           {
               name=>"jimi hendrix",
               instrument=>"guitar",
               genre=>"rock"
           },
           {
               name=>"miles davis",
               instrument=>"trumpet",
               genre=>"jazz"
           },
           {
               name=>"ella fitzgerald",
               instrument=>"vocal",
               genre=>"jazz"
           },
        ];
        push @$artists,{name=>"yoyo ma", instrument=>"cello",genre=>"classic"};
        foreach my $artist (@$artists){
            message("$artist->{name},$artist->{instrument},$artist->{genre}");
        }
    sub main
    {

        my $var = "This is the Perl 5 references chapter.";
        message($var);
    }

    sub message
    {
        my $m = shift or return;
        print("$m\n");
    }

    sub error
    {
        my $e = shift || 'unknown error';
        print("$0: $e\n");
        exit 0;
    }

    更多内容还是参考intermediate perl或者programming perl第三版。

  • 相关阅读:
    CodeForces 385D: Bear and Floodlight
    UVA
    SGU 495: Kids and Prizes
    CodeForces 148D: Bag of mice
    HDU 4405: Aeroplane chess
    HDU 4336: Card Collector
    UVA
    POJ 2577: Interpreter
    伪类选择器 伪原色选择器 选择器的优先级
    复习html CSS选择器 组合选择器和属性选择器
  • 原文地址:https://www.cnblogs.com/hanleilei/p/2389524.html
Copyright © 2011-2022 走看看