Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You have problems because you neglect to decode binary data to Perl strings during input and encode Perl strings to binary data during output. The reason for this is that regular expressions and its friend <code>split</code> work properly on Perl strings.</p> <p><code>(?&lt;=.)</code> means "after the first character". As such, this program will not work correctly on 复姓/compound family names; keep in mind that they are rare, but do exist. In order to always correctly split a name into family name and given name parts, you need to use a dictionary with family names.</p> <p>Linux version:</p> <pre><code>use strict; use warnings; use Encode qw(decode encode); while (my $full_name = &lt;DATA&gt;) { $full_name = decode('UTF-8', $full_name); chomp $full_name; my ($family_name, $given_name) = split(/(?&lt;=.)/, $full_name, 2); print encode('UTF-8', sprintf('The full name is %s, the family name is %s, the given name is %s.', $full_name, $family_name, $given_name) ); } __DATA__ 张小三 </code></pre> <p>Output:</p> <pre><code>The full name is 张小三, the family name is 张, the given name is 小三. </code></pre> <p>Windows version:</p> <pre><code>use strict; use warnings; use Encode qw(decode encode); use Encode::HanExtra qw(); while (my $full_name = &lt;DATA&gt;) { $full_name = decode('GB18030', $full_name); chomp $full_name; my ($family_name, $given_name) = split(/(?&lt;=.)/, $full_name, 2); print encode('GB18030', sprintf('The full name is %s, the family name is %s, the given name is %s.', $full_name, $family_name, $given_name) ); } __DATA__ 张小三 </code></pre> <p>Output:</p> <pre><code>The full name is 张小三, the family name is 张, the given name is 小三. </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload