You can simplify the code by removing the special cases for the two-character combinations and just use a regex. Just make sure you try to match the longer "characters" first, so their parts aren't matched instead.

Also, I used XML::LibXML to parse the structure.

#!/usr/bin/perl use warnings; use strict; use utf8; use XML::LibXML; my $file = shift; my %to_cyrilic = ( # Insert the hash definition here, see below. ); my $regex = join '|', sort { length $b <=> length $a } keys %to_cyrili +c; my $dom = 'XML::LibXML'->load_html( location => $file ); for my $text ($dom->findnodes('//text()')) { my $etext = $text; $text->setData($etext) if $etext =~ s/($regex)/$to_cyrilic{$1}/g; } print $dom;

Note that PRE tags preserve non-latin1 characters.

    F => 'Ф',
    H => 'Х',
    N => ':',
    Nj => 'Њ',
    b => 'б',
    c => 'ц',
    d => 'д',
    e => 'e',
    h => 'х',
    i => 'и',
    l => 'л',
    m => 'м',
    n => 'н',
    nj => 'њ',
    p => 'п',
    r => 'р',
    s => 'с',
    t => 'т',
    u => 'у',
    v => 'в',
    z => 'з',
    ć => 'ћ',
    Č => 'Ч',
    č => 'ч',
    Đ => 'Ђ',
    đ => 'ђ',
    Š => 'Ш',
    š => 'ш',
    Ž => 'Ж',
    ž => 'ж',
    # etc., this is enough to run the example.
($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

In reply to Re^6: Begginer's question: If loops one after the other. Is that code correct? by choroba
in thread Begginer's question: If loops one after the other. Is that code correct? by predrag

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.