Hi all !
I really need your help for a hint :
I need the length of strings which are UTF-8 coded.
I'm using Perl from Active state (5.8.8.822/Windows XP or 5.10.0.1004/Suse Linux 11.0) with the same "false" result.
(I think I'm wrong, but i can't find where).

for example:
length('a') returns 1 : That's right
length('à') returns 2 : That's wrong (I think it should return 1, because it's one CHARACTER long, although 2 BYTES because of UTF-8):

When looking at documentation (perlfunc / length) : I read that :
" Note the characters: if the EXPR is in Unicode, you will get the number of characters, not the number of bytes.
To get the length of the internal string in bytes, use bytes::length(EXPR), see the bytes manpage.
Note that the internal encoding is variable, and the number of bytes usually meaningless.
To get the number of bytes that the string would have when encoded as UTF-8, use length(Encoding::encode_utf8(EXPR))"

It seems to play the opposite role!
Any Idea ?
Thanks in advance
Didier

In reply to function length() in UTF-8 context by didess

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.