Macros and Unicode

Have a problem? A question? This is the place for answers from other Express users.
Post Reply
Steve
Posts: 17
Joined: 2004-11-11 08:58:19

Macros and Unicode

Post by Steve »

Perl People,

I am trying to write a simple perl macro to find and replace characters using Unicode hex values (i.e. 0x1E3D) but I can't seem to be able to enter 2-byte hex values. For instance, '$line =~ s/\x41/\x42/g' works but '$line =~ s/\x0041/\x0042/g' does not.

Is there any way to get these macros to work with such values so that I can do Unicode find-and-replace macros? I'm probably missing something but I can't figure out what.

I've got Perl v5.8.1-RC3 installed on my machine if that makes any difference.

Thanks,
STeve.
Steve
Posts: 17
Joined: 2004-11-11 08:58:19

Post by Steve »

I think I found the relevant piece of info, for anybody else who is dealing with, or might deal with, this situation. Before we print the converted text back out we include the line:

binmode STDOUT, ":utf8";

which apparently tells perl that it's OK to output wide characters. Thus, to change 'A' to A with a ring underneath, and 'B' to B with a line below we would use the following macro (stolen from Nisus and modified for our purposes):

#Nisus Macro Block
#source clipboard
#destination clipboard
#Before Execution
#Clipboard 1
#Copy
#After Execution
#Paste
#Clipboard 0
#End Nisus Macro Block
#This macro template lets you replace text with other text
#You can use regular expressions to do text pattern matching
binmode STDOUT, ":utf8";
while (my $line = <>) { #Reads each line of the selection from the clipboard
$line =~ s/\x{0041}/\x{1E00}/g; #A to A with under ring
$line =~ s/\x{0042}/\x{1E06}/g; # B to B with under bar

# Replace the words with the expressions
# Repeat the whole line starting "$line =~ " above for multiple replacements
print $line; #writes out each line to the clipboard
}

Admittedly, I don't understand all the comments preceding the code, since all we do is select all the text then choose our macro from the Macro menu. But, hey, I'm just evaluating this thing right now. However, with this little piece of the puzzle solved, I think I can recommend NWE to the the ancient linguistics dept.

Thanks for a really cool product!

Steve.
Post Reply