English 中文(简体)
Perl: Define hash with reference to the same hash,$this->{key}?
原标题:Perl: Define hash with reference to the same hash, $this->{key}?
  • 时间:2010-01-27 16:53:13
  •  标签:
  • perl

我怎么会像以下那样 has:

my %hash = (key1=>"Something", key2=>$hash{key1} . "Else");

在我宣布 has的时候,这怎么办? 迄今为止,我提出的唯一一件事是:

my %hash = (key1=>"Something");
$hash{key2} = $hash{key1} . "Else";
最佳回答

为什么不使用中间变量?

my $common_value =  Something ;
my %some_hash = ( k1 => $common_value, k2 => $common_value. Else  );

<>Update>

kemp询问为什么我们需要一个中间变量。 我可以解释这一问题的两种方式,我恳求回答这两种解释。

  1. Why can t You do my %hash = ( k1 => foo , k2 =>$hash{k1}. ;?

    右侧(rhs)在左侧(lhs)之前接受评估。 因此,当确定<代码>k2的价值时,对<代码>%hash无改动。 至今尚未发生。 事实上,%hash甚至尚未建立,并已输入到 s垫。 (%hash is a lexical variability here, so that it found t go into the sign table.)

  2. Why使用一种变量而不是另一种方法?

    诚然,有许多办法以这种价值观为起点。 如果我有少量相关钥匙可以启动,我将使用中间变量。

    my $cv1 = sub_result() * 5;
    my $cv2 = "Number: $cv1";
    
    my %h = (
        k1 => $cv1,  
        k2 => $cv2,
        k3 => "Numero: $cv1",
        k4 => "Big $cv2",
        k5 => "Round $cv2",
        k6 => "Whole $cv2",
        k7 => "-$cv1",
    );
    

    然而,如果我不得不建立许多依赖许多不同关键因素的复杂价值观,我或许会采用一种数据驱动的方法,来初步确定该散盘。 具体执行取决于我没有掌握的细节,但可以看一下:

    use Scalar::Util qw(reftype);
    
    my @init = ( 
        [ key1 => $value                 ],
        [ key2 => &make_a_value,  key1  ],
        [ key3 => &another_way,   key2  ],
    );
    
    my %h; 
    for my $spec ( @init ) {
        my $key   = shift @$spec;
        my $value = shift @$spec;
        my @args  = @$spec;
    
        if( reftype $value eq reftype sub {} ) {
            $value = $value->( @h{ @args } );
        }
    
        $h{$key} = $value;
    }
    

    对于任何涉及这一问题的案件,如何使用技术的选择取决于该案例所特有的许多细节。 因此,我们必须作一般性发言。 中间变量是解决欧佩斯问题的好办法。 我不能说,这是否应该用于他的具体情况。

问题回答

由于以下两个原因不能做到这一点:

  1. 在Perl程序key2 =>hash{key1}时,它尚未将分配给关键1的记忆与散列名称。 换言之,<代码>$hash{key1} 在<代码>key2 =>hash{key1}已分配到之前,不指明记忆中的正确位置。

  2. 以前的理由没有说明另一个问题:到你处理<代码>%h/code>时,你就把该编号列入一个编号表:<代码>key2 =>hash{key1}。 (按行文/评价顺序排列),如果您的<代码>严格使用,则汇编者将予以禁止;按您的一贯做法。 这一点很容易与先前声明的“my%hash

At the point where you are defining the hash, $hash{key1} does not exist, so you can t do this in one statement. You can do your own solution and use key key1 after you have defined it.

错误

my %hash = ( key1 => "Something", key2 => "Something" . "Else" );

?

是的,我知道这些人是谁,但是,在你建造洗衣时,他们知道什么(或你根本不建造),因此,你为什么不加入他们?

首先,我只是以中间价值这样做。 但是,你有两个更先进的选择。 如果你重新 new新,我不建议其中的(或)——事实上,我喜欢最好的中间价值解决办法,而是完全:

  • You can use Tie::Hash which allows you to make an object accessible with hash syntax and hash style dereferencing (->{key1}), you can find more information on this method with perldoc perltie, and the man page linked above for Tie::Hash;
  • You can abandon the idea of using a hash; and, use an object, or even Moose which will allow you to initialize the object from a hash, sharing the same value. I don t see much purpose in a real world example in doing this.

这里就是一个莫斯例子。

package Class;
use Moose;
has [qw/key1 key2/] => ( isa =>  Str , is =>  rw , init_arg =>  key1  );

my $o = Class->new( { key1 =>  foobar  } );
print $o->key1, $o->key2;

你们可以相当接近,但我确信,除了新颖外,这并不具有价值:

$_->{key2} = $_->{key1} . "Else" for my $hash = { key1 => "Something" };




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...