2つのハッシュテーブルに一致する必要がありますが、そのキーが一致しません。だから私は各ハッシュテーブルに到達し、値で一致します。だから、次のコードは動作しますしません:perlでハッシュテーブルにアクセス
sub _findHashElt
{
my ($this, $hashTarget) = @_;
my $isFound = 0;
my $isItemFound = 1;
my $isHashFound = 0;
my $result = undef;
my %hashTable=%{$this->{templates}};
my %hashTargetHash=%{$hashTarget};
my %hashSubTable = undef;
while ( (my ($key, $value) = each(%hashTable))
and ($isFound == 0 ))
{
$isItemFound = 1;
$isHashFound = 0;
####################################
# DOES NOT WORK
%hashSubTable = %{$hashTable{$key}};
####################################
while ( (my ($subKey, $subValue) = each(%hashTargetHash))
and ($isItemFound == 1 ))
{
$isItemFound = 0;
while ( (my ($subTableKey, $subtableValue) = each(%hashSubTable))
and ($isItemFound == 0 ))
{
$isHashFound = 1;
$isItemFound = ($subValue eq $subtableValue) ? 1 : 0;
}
}
if ($isItemFound == 1 && $isHashFound == 1) {
$isFound = 1;
$result = $key;
}
}
return $result;
}
とコードを次のように動作します。
sub _findHashElt
{
my ($this, $hashTarget) = @_;
my $isFound = 0;
my $isItemFound = 1;
my $isHashFound = 0;
my $result = undef;
my %hashTable=%{$this->{templates}};
my %hashTargetHash=%{$hashTarget};
my %hashSubTable = undef;
while ( (my ($key, $value) = each(%hashTable))
and ($isFound == 0 ))
{
$isItemFound = 1;
$isHashFound = 0;
while ( (my ($subKey, $subValue) = each(%hashTargetHash))
and ($isItemFound == 1 ))
{
####################################
# WORK
%hashSubTable = %{$hashTable{$key}};
####################################
$isItemFound = 0;
while ( (my ($subTableKey, $subtableValue) = each(%hashSubTable))
and ($isItemFound == 0 ))
{
$isHashFound = 1;
$isItemFound = ($subValue eq $subtableValue) ? 1 : 0;
}
}
if ($isItemFound == 1 && $isHashFound == 1) {
$isFound = 1;
$result = $key;
}
}
return $result;
}
誰かが、問題は何お願いし、私に言うことができますしてください?
EDIT
まず...
EDITは最初のケースで
#############################################
################# TEST PART #################
#############################################
my $this = {"templates" => {}};
my $example = {'key0' => {'key00' => 'test00', 'key01' => 'test01', 'key02' => '0', 'key03' => 'test03'},
'key1' => {'key00' => 'test10', 'key01' => 'test11', 'key02' => '1', 'key03' => 'test13'},
'key2' => {'key00' => 'test20', 'key01' => 'test21', 'key02' => '1', 'key03' => 'test23'},
'key3' => {'key00' => 'test30', 'key01' => 'test31', 'key02' => '0', 'key03' => 'test33'}};
my $expected = {'key00' => 'test00', 'key01' => 'test01', 'key02' => '0', 'key03' => 'test03'};
$this->{templates}=$example;
_findHashElt($this, \%expected);
、キーが発見されておらず、第二にキーが見出されていますケース:
$ perl myTest.pl
Key not found
第二の場合:
$ perl myTest.pl
Key found
{
key01 : test01
key00 : test00
key03 : test03
key02 : 0
}
EDIT
もう一つは、私はそれが
$ perl --version
This is perl, v5.10.1 (*) built for i686-cygwin-thread-multi-64int
(with 13 registered patches, see perl -V for more detail)
通常、問題を最小限に抑えることをお勧めします。 –
私は最小限のバージョンに私の問題を沸かせる方法を知りません。しかし、私はそれを助けることができる完全な例を追加することができます。 –
'does not work'とはどういう意味ですか?入力と予想される出力のサンプルデータを提供してください。 –