0

次のように初期化された Tie::IxHash オブジェクトがあります。

my $ixh = Tie::IxHash->new('a' => undef, 'b' => undef, 'c' => undef);

後で、これら 3 つのキーに値のリストを割り当てたいと思いqw/1 2 3/ます。1つのステートメントでそれを行う方法を見つけることができないようです。

(あるステップでキーを割り当て、別のステップで値を割り当てる理由は、これが API の一部であり、代わりにユーザーが (キー、値) インターフェイスを使用して値を追加したい場合があるためです。)

試してみ$ixh->Values(0..2) = qw/1 2 3/;ましたが、その方法は左側にいるのが好きではありません。

もちろん、$ixh->Replace(index, value)を使用してループを作成することもできますが、見落としている「一括」メソッドがあるかどうか疑問に思いました。

4

2 に答える 2

3
tie my %ixh, Tie::IxHash::, ('a' => undef, 'b' => undef, 'c' => undef);
@ixh{qw( a b c )} = (1, 2, 3);

But it's not really a bulk store; it will result in three calls to STORE.


To access Tie::IxHash specific features (Replace and Reorder), you can get the underlying object using tied.

tied(%ixh)->Reorder(...)

The underlying object is also returned by tie.

my $ixh = tie my %ixh, Tie::IxHash::, ...;
于 2013-06-10T00:04:33.407 に答える
1

Does this mean I couldn't use the "more powerful features" of the OO interface?

use strict;
use warnings;
use Tie::IxHash;

my $ixh = tie my %hash, 'Tie::IxHash', (a => undef, b => undef, c => undef);

@hash{qw(a b c)} = (1, 2, 3);

for ( 0..$ixh->Length-1 ) {
  my ($k, $v) = ($ixh->Keys($_), $ixh->Values($_));
  print "$k => $v\n";
}

__OUTPUT__
a => 1
b => 2
c => 3
于 2013-06-10T00:49:50.350 に答える