At first, I'm sorry that I'm not good at English.
To represent a Unicode character in a string or in a regexp, I can use
"\x{hex}" notation.
my $char = "\x{AC00}";
# $char = "가" -- a Korean character, pronounced "GA"
(I'm not sure you can see this Korean character in your browser.
Please tell me if you can't)
However, it seems that this representation works only when it is hard-
coded. That means, I can't use a variable for the hex value:
my $index = "AC00";
my $char = "\x{$index}"; # This doesn't work.
print length($char),"\n";
print "[$char]\n";
> ./test.pl
1 -- $char has one character but...
[] -- that character is not "가"(GA). It isn't even a printable
character.
(In fact, $char seems to be null char "\0". I found it by redirecting
the output into a file and viewing the file with hex editor)
Anyway, I tried several codes including double quote, single quote,
s/// op, etc.
Finally I found the code that works:
(code)
#!/usr/bin/perl
my $index = "AC00";
my $char = eval( "\"\\x{$index}\"" );
print length($char),"\n";
print "[$char]\n";
(output)
> ./test.pl
1
Wide character in print at ./test.pl line 6.
[가]
I had to make a string that consists of
double quote " (it must be quoted with backslash)
backslash \ (quoted)
x
brace {
Unicode index
brace }
double quote " (quoted)
Then I have to eval that string... This is, I think, so complicated.
I think there may be a better way to do this. I found that
Unicode::Char module provides u() subroutine:
my $u = Unicode::Char->new();
my $char = $u->u('AC00'); # u() returns a character of Unicode
index AC00
( http://search.cpan.org/~dankogai/Unicode-Char-0.02/lib/Unicode/Char.pm
)
But I still wonder if there is a Perl internel function or standard
module that do same thing. I want to know what is the most popular
way.
Thanks.
G.Y.Park from South Korea.
[...]
> However, it seems that this representation works only when it is hard-
> coded. That means, I can't use a variable for the hex value:
>
> my $index = "AC00";
> my $char = "\x{$index}"; # This doesn't work.
[...]
> Finally I found the code that works:
[...]
> my $index = "AC00";
> my $char = eval( "\"\\x{$index}\"" );
[...]
> Then I have to eval that string... This is, I think, so complicated.
>
> I think there may be a better way to do this. I found that
> Unicode::Char module provides u() subroutine:
>
> my $u = Unicode::Char->new();
> my $char = $u->u('AC00'); # u() returns a character of Unicode
> index AC00
> (http://search.cpan.org/~dankogai/Unicode-Char-0.02/lib/Unicode/Char.pm
> )
>
> But I still wonder if there is a Perl internel function or standard
> module that do same thing.
perldoc -f chr
perldoc -f oct
the easiest would be:
my $index = "AC00";
my $char = chr(oct("0x$index"));
print length($char),"\n";
print "[$char]\n";
--
Klaus
Obviously you need a Korean font to view Korean characters. As I don't
have a Korean font installed, obviously I can't see it.
>However, it seems that this representation works only when it is hard-
>coded. That means, I can't use a variable for the hex value:
>
>my $index = "AC00";
>my $char = "\x{$index}"; # This doesn't work.
Right. And it's not "hardcoded", but think of it as a notation for a
character.
If you do
$wh = 'wh';
{$wh}ile (someCondition) {...}
then you don't get a while loop, either.
>my $char = eval( "\"\\x{$index}\"" );
Arggg, that's ugly!
>I think there may be a better way to do this. I found that
perldoc -f chr
jue
Or:
my $char = chr hex $index;
> print length($char),"\n";
> print "[$char]\n";
John
--
Those people who think they know everything are a great
annoyance to those of us who do. -- Isaac Asimov
Oops, "chr" can receive Unicode index as its argument.
I've thought it accepts only bytes (0~255)... I'm so sorry for
bothering you.
Thank you all.
G.Y.Park in South Korea
Or:
my $index = 0xAC00;
my $char = chr $index;
Though if you have the index as a string, you'll need to use hex().
--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"