Why can't I use the map function to create a good hash from a simple data file in Perl?

Message updated. Please kindly skip to the solution part if you have already read the posted question. Thanks!

Here is the code below to show my problem:

The input data file for the test was saved using the Notepad built-in notepad in the form of UTF-8 encoding. It has the following three lines:

abacus æbәkәs
abalone æbәlәuni
abandon әbændәn

The Perl script file was also saved using the built-in Notepad window as UTF-8 encoding. It contains the following code:

#!perl -w

use Data::Dumper;
use strict;
use autodie;
open my $in,'<',"./hash_test.txt";
open my $out,'>',"./hash_result.txt";

my %hash = map {split/\t/,$_,2} <$in>;
print $out Dumper(\%hash),"\n";
print $out "$hash{abacus}";
print $out "$hash{abalone}";
print $out "$hash{abandon}";

On the output, the hash table looks fine:

$ VAR1 = {
          'abalone' => 'æbәlәuni
',
          'abandon' => 'әbændәn',
          'Abacus' =>' æbәkәs
''
        };

, :

æbәlәuni
әbændәn

Perl :

Use of uninitialized value $hash{"abacus"} in string at C:\test2.pl line 11, <$i n> line 3.

? - ? .

, :) , , , :) @Sinan, 100% , , , , , UTF-8 - Perl . , "<: utf8" " > : utf8" , , utf-8 . .

, , Perl :

#!perl -w

use Data::Dumper;
use strict;
use autodie;

open my $in,'<',"./hash_test.txt";
open my $out,'>',"./hash_result.txt";

seek $in,3,0; # force Perl to ignore the BOM!
my %hash = map {split/\t/,$_,2} <$in>;
print $out Dumper(\%hash);
print $out $hash{abacus};
print $out $hash{abalone};
print $out $hash{abandon};

- , :

$VAR1 = {
          'abalone' => 'æbәlәuni
',
          'abandon' => 'әbændәn',
          'abacus' => 'æbәkәs
'
        };
æbәkәs
æbәlәuni
әbændәn

, script UTF-8, utf-8, UTF-8.

, . , @, . , , .

, :

open my $in,'<:utf8',"./hash_test.txt";
open my $out,'>:utf8',"./hash_result.txt";

my %hash = map {split/\t/,$_,2} <$in>;
print $out Dumper(\%hash);
print $out $hash{abacus};
print $out $hash{abalone};
print $out $hash{abandon};

:

$VAR1 = {
          'abalone' => "\x{e6}b\x{4d9}l\x{4d9}uni
",
          'abandon' => "\x{4d9}b\x{e6}nd\x{4d9}n",
          "\x{feff}abacus" => "\x{e6}b\x{4d9}k\x{4d9}s
"
        };
æbәlәuni
әbændәn

:

Use of uninitialized value in print at C:\hash_test.pl line 13,  line 3.
+3
5

. , $in 3, 4 .

, GVim, UTF-8, . , Notepad, , :

"\x{feff}abacus" => "\x{e6}b\x{4d9}k\x{4d9}s
"

\x{feff} - .

Dumper abacus ( :utf8 ) .

( , hobbs), '<:utf8', .

+7

/ UTF8, , UTF8.

#! /usr/bin/env perl
use Data::Dumper;
open my $in,  '<:utf8', "hash_test.txt";
open my $out, '>:utf8', "hash_result.txt";

my %hash = map { chomp; split ' ', $_, 2 } <$in>;
print $out Dumper(\%hash),"\n";
print $out "$hash{abacus}\n";
print $out "$hash{abalone}\n";
print $out "$hash{abandon}\n";

, , :encoding(utf8) :utf8 .

open my $in, '<:encoding(utf8)', "hash_test.txt";

PerlIO.

+2

, . Data::Dumper, :

$VAR1 = {
          'abalone' => 'æbәlәuni
',
          'abandon' => 'әbændәn',
          'abacus' => 'æbәkәs
'
        };

' abacus? $hash{abacus}. - abacus Dumper(). , :

foreach my $k (keys %hash) {
  print $out $hash{$k};
}
+1

split/\ s/ split/\ t/

0

. , ?

-1
source

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


All Articles