A tip for next time; those who post code get more thorough responses.

It's not really a translation/transliteration problem. tr/// passes through a string a character at a time, transliterating based upon your tr/source-class/dest-class/. For example, tr/A-Z/a-z/ lowercases a string, because it goes through each char in a string, and if it is in the class A-Z, it gets translated to its companion in the class a-z.

What you want is a substitution: s///

$string = 'polar bear'; $string = uc $string; ## make all-uppercase $string =~ s/.{3}$/lc $&/e; ## lowercase 3 chars at end

At the end of that block, $string will contain 'POLAR Bear'.

That regex is a little confusing, but it's actually quite simple. The .{3} says "match any set of exactly 3 chars", and the $ says "at the end of the string". As for the second half, lc $& calls the lc function on the variable $&, which will contain the matched chars -- in this case, the last three of the string. The e on the end tells the substitution to eval the second part, otherwise it would replace the last three chars with 'lc ' and the chars themselves: 'POLAR BEAR' would become 'POLAR Blc EAR'.

I assumed you meant "uppercase all but the last three chars", which is what your examples seemed to say. If you meant "uppercase the first N, lowercase the last 3, and leave the rest alone, you could do this:

$string = 'polar BEAR'; $N = 4; $string =~ s/^.{$N}/uc $&/e; ## uppercase $N chars at start $string =~ s/.{3}$/lc $&/e; ## lowercase 3 chars at end

This would result in 'POLAr Bear'. The regex works similar to the one above, but ^ says "at the beginning of the string".

Anima Legato
.oO all things connect through the motion of the mind


In reply to Re: string translation by legato
in thread string translation by Anonymous Monk

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.