Вопрос

I am new to perl, I am learning using O'reillys Learning Perl book. Unicode characters(as written in the book) like this can be printed using print "\x{2668}". I was using foreach loop to print a range of Unicode characters.

Example: from 2660 - 2670;

I have written the following code,

foreach $num (2660..2670)
{
        print "Character for $num  is:","\x{$num} \n";

}

Which was giving the following output,

Illegal hexadecimal digit '$' ignored at ./Printing_messages.pl line 10.
Character for 2660  is: 
Character for 2661  is: 
Character for 2662  is: 
Character for 2663  is: 
Character for 2664  is: 
Character for 2665  is: 
Character for 2666  is: 
Character for 2667  is: 
Character for 2668  is: 
Character for 2669  is: 
Character for 2670  is:

After that I've made some other tweaks, but nothing worked. What is the problem with the my program ? I don't understand. Is there how to make it work ?

Это было полезно?

Решение

You want

print "Character for $num is: ", chr($num), "\n";

Другие советы

You can write something like :

foreach $num (2660..2670)
{
        print "Character for $num  is:",eval(qq!\"\\x{$num} \\n\"!);
}

The problem with your original program, is that perl cannot evaluate "\x{...}" because this evaluation is done before the interpolation of $num.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top