Perl needs to know that you are dealing with Unicode characters. Consider:
#!/usr/bin/perl use strict; use warnings; use utf8; my $str = "ö123 å456 å789"; open my $inFileUTF, '<:utf8', \$str; my @lines = <$inFileUTF>; chomp @lines; @lines = map {scalar reverse} @lines; print "Using utf8\n"; print "$_\n" for @lines; open my $inFile, '<', \$str; @lines = <$inFile>; chomp @lines; @lines = map {scalar reverse} @lines; print "\nUsing default file I/O\n"; print "$_\n" for @lines;
Prints:
Using utf8 321ö 654å 987å Using default file I/O 321¶Ã 654¥Ã 987¥Ã
Note the use of both strict and warnings. Always use strictures.
Note the use of three parameter open and lexical file handles (usual file checking omitted because of the string trick).
The \$str syntax lets me open the string as though it were a file. Very useful here to provide the data in the same "file" as the script.
See PerlIO for a description of the open custom layer options - the :utf8 bit.
In reply to Re: Creating reverse dictionary
by GrandFather
in thread Creating reverse dictionary
by fish496
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |