Basically, I'm trying to write a snipette of code (to be included in a larger script) to do Unicode entitiy substitution.
The basic idea is to search for any entities, then look them up in a hash (in pracice, created from an external file). If the entitiy is present, replace the entity with the unicode decimal code, otherwise leave the entity alone.
Here is my current state of progress:
#!/usr/bin/perl
# Unicode entity text => Unicode decimal number
%lookup = ("Adieresis" => 196,
"Aring" => 197,
"Ccedilla" => 199,
"Eacute" => 201,
"Ntilde" => 209,
"Odieresis" => 214
);
# A few lines of text to test.
# Only elements 4 and 6 should match
@source = ("fred",
"adieresis",
"Adieresis",
"&adieresis;",
"&Adieresis;",
"",
"fr&Adieresis;ed"
);
foreach (@source) {
# regexp: [^;]+? matches 1+ characters which are not a semi-colon
# ([^;]+?) Remember it (in $1)
# &([^;]+?);? basically matches a (pseudo) entity
s/&([^;]+);?/"&#".eval(exists $lookup{\1} ? $lookup{\1} : \1).";"/e;
print "($1) $_\n";
}
This outputs:
() fred
() adieresis
() Adieresis
(adieresis) &#;
(Adieresis) &#;
(Adieresis)
(Adieresis) fr&#;ed
which is not what I'm after.. it's finding matches, but not replacing it with anything
the eval part, basically
I'm sure that subsitution (either with s/// or tr///) should be able to do it, but I'm just not getting it.. :-(
Anyone got anything that works?
(am I barking at the wrong tree?)
(should I even be barking here?)
In hope....
-- Ian Stuart
A man depriving some poor village, somewhere, of a first-class idiot.