Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

OK. Slapping ideas together, and extracting commonalities, from among the responses so far and combining with constructing a random string, I tried the following experiment.

use strict; use warnings; use Math::Random::MT::Auto::Range; binmode(STDOUT, ":utf8"); $| = 1; #the following exhausts all memory #my $all = join q[], grep /^\w$/, map chr, shuffle 0 .. 0x10FFFF; #print $all,"\n"; # # so I tried: my @chars = map(chr, 0 .. 0x10FFFF); my $rng = Math::Random::MT::Auto::Range->new(LO => 0, HI => 0x10FFFF, TYPE => 'INTEGER' +); # three options for doing the basically the same thing for (my $i = 0 ; $i < 10 ; $i++) { my $code = $rng->rrand; print "$i <=> $code <=>",chr($code),"\n"; print "\t$i <=> $code <=>",$chars[$code],"\n"; print "\t$i <=> $code <=>",pack("U",$code),"\n"; }

There are a couple problems with this. chr generates some errors and warnings. There are a couple thousand instances of "UTF-16 surrogate 0xd81b at c:/Work/test.utf8.latin1.pl line 16.", and over a hundred instances of "Unicode non-character 0xfdd0 is illegal for interchange at c:/Work/test.utf8.latin1.pl line 16.", each for a different integer. Line 16 is the line where @chars is initialized. I suppose a couple thousand problem characters in @chars is not a huge issue, but given that I want to use it to test functions for converting from utf-8 to latin1 and back, I expect that if they happen to occur in my sample, there'd be some false indications of errors in these functions (from the Perl package 'Encode').

The second problem is that though I put the statement "binmode(STDOUT, ":utf8");" at the start of the script, the printout contains only rectangles and square where the UTF-8 character ought to be; at least when I execute within Emacs. When I execute the script in the Windows commandline terminal, I invariably get gibberish (different for each character) the width of four characters. How, then, do I actually see the characters? I thought, probably mistakenly, I'd see a few greek or sanskrit characters or characters from other alphabets.

What I was thinking of doing is amend the loop I show above to construct a single string from the ten characters produced, and then use the functions from 'Encode' to convert it to latin1, and then back to utf-8, to see if the input string and the output string are the same (and if not, then this whole idea is doomed to fail). I'd repeat this test for a few million random utf-8 strings, and if there are no failures, then I could use this idea as a temporary measure until I can test our systems to see how best to adapt to use of utf-8 throughout.

What, then, do I do to exclude the integers that either result in a utf-16 surrogate and those that represent illegal characters? And is it possible for me to actually see the characters produced?

Thanks

Ted


In reply to Re: How to generate random sequence of UTF-8 characters by ted.byers
in thread How to generate random sequence of UTF-8 characters by ted.byers

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (5)
    As of 2014-12-29 06:23 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (184 votes), past polls