我怎么会像以下那样 has:
my %hash = (key1=>"Something", key2=>$hash{key1} . "Else");
在我宣布 has的时候,这怎么办? 迄今为止,我提出的唯一一件事是:
my %hash = (key1=>"Something");
$hash{key2} = $hash{key1} . "Else";
我怎么会像以下那样 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询问为什么我们需要一个中间变量。 我可以解释这一问题的两种方式,我恳求回答这两种解释。
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.)
诚然,有许多办法以这种价值观为起点。 如果我有少量相关钥匙可以启动,我将使用中间变量。
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;
}
对于任何涉及这一问题的案件,如何使用技术的选择取决于该案例所特有的许多细节。 因此,我们必须作一般性发言。 中间变量是解决欧佩斯问题的好办法。 我不能说,这是否应该用于他的具体情况。
由于以下两个原因不能做到这一点:
在Perl程序key2 =>hash{key1}
时,它尚未将分配给关键1的记忆与散列名称。 换言之,<代码>$hash{key1} 在<代码>key2 =>hash{key1}已分配到之前,不指明记忆中的正确位置。
以前的理由没有说明另一个问题:到你处理<代码>%hmy%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新,我不建议其中的(或)——事实上,我喜欢最好的中间价值解决办法,而是完全:
->{key1}
), you can find more information on this method with perldoc perltie
, and the man page linked above for Tie::Hash
;这里就是一个莫斯例子。
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" };
如果制造中间变量outside of %hash is offting,那么使用> ,可使其更符合情理:
my %hash = (
do {
my $s = Something ;
( key1 => $s, key2 => $s . Else );
},
);
页: 1
I am building a Web interface to monitor an embedded system. I have built a Perl script which runs remote commands and gathers output from that system. Now what I need is a Web interface which makes ...
How do I tell what type of value is in a Perl variable? $x might be a scalar, a ref to an array or a ref to a hash (or maybe other things).
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: "...
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 ...
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 ...
I would like to submit a form to a CGI script localy (w3c-markup-validator), but it is too slow using curl and apache, I want to use this CGI script more than 5,000 times in an another script. and ...
So I m running perl 5.10 on a core 2 duo macbook pro compiled with threading support: usethreads=define, useithreads=define. I ve got a simple script to read 4 gzipped files containing aroud 750000 ...
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 ...