Hash value overwritten due to similar keys?

I want to display data from a German data structure into English.

To do this, I use a hash that holds German words as keys, and English as values ​​($ mapping_table).

Data is stored in a hash array ($ data). The keys are German words that need to be replaced with English. Values ​​are data that remains unchanged.

To do the mapping, I wrote the following code:

my $mapping_table = {
    'Exemplare' => 'copies',
    'Seiten' => 'pages',
    'Statushinweis' => 'status',
    'Serie von' => 'number_of',
    'ISBN/Barcode-Nr.' => 'ISBN_barcode',
    'Status' => 'status',
};

my $data = [
  {
    'Exemplare' => '1',
    'Seiten' => '0',
    'Statushinweis' => 'Statushinweis',
    'ISBN/Barcode-Nr.' => '3-551-01561-9',
    'Serie von' => '4',
    'Status' => 'Gesucht'
  },
  {
    'Exemplare' => '4',
    'Seiten' => '111',
    'Statushinweis' => '',
    'ISBN/Barcode-Nr.' => '3-551-01561-9',
    'Serie von' => '4',
    'Status' => 'Vorhanden'
  }
];

my $mapped_data = [];

foreach my $issue ( @$data ) {
    my %tmp_hash;
    foreach my $key (sort keys %$mapping_table) {
        $tmp_hash{$mapping_table->{$key}} = $issue->{$key};
    }
    push @$mapped_data, \%tmp_hash;
}

print Dumper $mapped_data;

The dump result is very surprising to me.

$VAR1 = [
          {
            'number_of' => '4',
            'copies' => '1',
            'status' => 'Statushinweis',
            'ISBN_barcode' => '3-551-01561-9',
            'pages' => '0'
          },
          {
            'pages' => '111',
            'ISBN_barcode' => '3-551-01561-9',
            'status' => '',
            'copies' => '4',
            'number_of' => '4'
          }
    ];

You can see that the key status values ​​hold the "Statushinweis" keys, while these keys are completely lost.

I tried to understand why this is happening with the help of Google and the debugger, but failed (maybe it is obvious that they can be seen?).

, , perl 5.26.1 Win7.

, ?

.

+4

Source: https://habr.com/ru/post/1692940/


All Articles