用另一个散列哈希替换散列哈希值

时间:2016-05-10 05:40:37

标签: perl hash replace key

我有两个哈希哈希,我是从两个独立的tex文件创建的。我想用哈希的第一个哈希值更改哈希的第二个哈希值,并希望将其写入第三个文件。或者我可以自己替换第二个文本文件中的值。

File1中:

family (){
kapoor(){
member1(){
male(){
between : 0 to 5
person : 2
their name is bla & bla. 
and bla bla......
blaa.....
}

male(){
between : 5 to 5
person : 20
their name is bla & bla bla. 
and bla bla......
blaa.....
}
}

member2(){
male(){
between : 0 to 5
person : 2
their name is bla & bla. 
and bla bla......
blaa.....
}

male(){
between : 5 to 5
person : 20
their name is bla & bla bla. 
and bla bla......
blaa.....
}
}
}


rai(){
member1(){
male(){
between : 0 to 5
person : 2
their name is bla & bla. 
and bla bla......
blaa.....
}

male(){
between : 5 to 5
person : 20
their name is bla & bla bla. 
and bla bla......
blaa.....
}
}

member2(){
male(){
between : 0 to 5
person : 2
their name is bla & bla. 
and bla bla......
blaa.....
}

male(){
between : 5 to 5
person : 20
their name is bla & bla bla. 
and bla bla......
blaa.....
}
}
}

HASH1:

family=>{

     kapoor => {
                member1=>{ 
                           0 to 5=> person : 2
                                    their name is bla & bla. 
                                    and bla bla......
                                    blaa.....

                          }   
               member2=> {
                          5 to 5 => person : 20
                                    their name is bla & bla bla. 
                                    and bla bla......
                                    blaa.....

                }

rai =>{
/...................etc
}

hash 2相同但值不同。

我试过了:

while(my$line=<$fh>)  ******** to store hashes of hash from file 1***
  {

  if($line=~/$family_name_re/)
  {
      $family_name=$1;
  $family_count=$cell_count+1;
  }
  elsif ($line=~/$member_re/)
  {
  $member = $1;
 }
  elsif ($line=~/$age_re/)
 {
  $age_range=$1;

 push @{$data{$family_name}{$member}{$age_range}{values}},$line;
  }
}
  print Dumper(\%data);
  print "Number of cell is $cell_count\n";

 close ($fh);

我从第二个文件读取并将其存储在哈希值2的哈希值中。

有什么建议我可以替换这个值吗? 我试图替换第二个文件,但它没有工作。

$_=~s/$newvalue/$data{$family_name}{$member}{$age_range}/;

我认为不是替换,而是编写file2,因为它在第三个文件中,而第一个文件中的值只能反映在第三个文件中。

1 个答案:

答案 0 :(得分:0)

您已经说过使用RegEx获得正确的值,所以只需将它们写回文件:

open my $fh3,'>','file3.txt';
print $fh3 "family (){\n";
for my $family (keys %data) {
    print $fh3 "$family () {\n";
    for my $member (keys %{$data{$family_name}}) {
        print $fh3 "$member () {\n";
        for my $age_range (keys %{$data{$family_name}}) {
            print $fh3 "$age_range () {\n".
                  join(
                      "\n",
                      @{$data{family_name}->{$member}->{$age_range}->{values}}
                  ).
                  "}\n";
        }
        print $fh3 "}\n";
    }
    print $fh3 "}\n";
}
print $fh3 "}\n";

循环遍历结构的所有级别,将所有内容打印到文件中。

另一次尝试:

sub write_part {
    my $fh = shift;
    my $data = shift;

    if (ref($data) eq 'HASH') {
        for my $key (keys %$data) {
            print $fh "$key () {\n";
            write_part($fh, $data->{$key});
            print $fh "}\n";
        }
    } elsif (ref($data) eq 'ARRAY') {
        print $fh join("\n", @$data)."\n";
    } else {
        print $fh $data."\n";
    }
}

open my $fh3, '>', 'file3.txt';
write_part($fh3, \%data);
close $fh3;

一旦传递最顶层的数据结构就调用sub,并在HashRef获得$data时调用它自己。