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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.