Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am writing a flash card program with PERL, and I have two problems:

How do I read from a file in the format:

[][][] \t pronunciation [][][] \t pronunciation

the [][][] basically represents any number of Unicode characters [in my case, Chinese], and the pronunciation is in regular test.

and secondly, how do I ensure that Unicode will work on this program:

open(INPUT,"<","c:\\software\\alexcode\\AlexData.txt") or die"Could not open input file for reading\n"; $count = 0; my @prehasha = (); while (<INPUT>) { chomp; my ($values1, $values2) = split(/\t/,$_); $prehasha[$count++] = $values1; $prehasha[$count++] = $values2; print "$values1 and $values2\n"; } #above this Mike wrote print "after while statement @prehasha \n"; #now we have an array where the first element is the char and the seco +nd element is the pinyin etc. @char = (); @pinyin = (); while (@prehasha != 0) { $a = shift @prehasha; $b = shift @prehasha; push (@char, $a); push (@pinyin, $b); #the array has officially been split! } print "@char : @pinyin\n"; keys %charpinyin = @char; #values %charpinyin = @pinyin; #Mike approved 8:44 PM $elnum = ($char)/2; #now is where we assign random numbers to each pair in the hash! $randomarray{$elnum-1} = (); foreach (@randomarray) { push (@random, int( rand($elnum-1) )-1) } #keys %randhash = 1..$elnum-1; #values %randomhash = @randomarray; #now we have two hashes, %charpinyin, and %randomarray, what I will do + now is sort the randomhash hash based on the random number, and use +the key [0..elnum-1] to determine which pair to spit off on the scree +n! clever, eh? #the following few lines is ONLY a test# while (($key, $value) = each(%charpinyin)) { print "$key \t $value \n"; }

20040806 Janitored by Corion: Fixed formatting

janitored by ybiC: Retitle from less-than-descriptive "Trouble", as one-word titles hinder site search

Replies are listed 'Best First'.
Re: Reading unicode characters from file
by Prior Nacre V (Hermit) on Aug 06, 2004 at 20:37 UTC

    If the file only contains tab-separated data you can read it easily like this:

    { local $/ = "\t"; while (my $data_frag = <FH>) { # process $data_frag here } }

    If you reformat the above, I'll look at the second part.

    Update
    While my statement above is correct, now that you have fixed the formatting, I see it is not applicable to your question.

    Regards,

    PN5

Re: Reading unicode characters from file
by graff (Chancellor) on Aug 07, 2004 at 18:35 UTC
    What do you mean by "unicode characters"? This is a multiple-choice type question. Possible choices are: UTF-16BE (aka UCS-2BE), UTF-16LE (aka UCS-2LE), utf8. (If you don't understand the question, you need to learn enough about unicode so that you do, or at least know enough about your input data to know which choice is correct.)

    You should be using a very recent version of Perl (5.8.1 or later), and you should look up the man pages "perluniintro", "perlunicode" and the "Encode" module.

    To read unicode data properly from a file or STDIN, (and to write it properly to a file or to STDOUT), the easiest way is to use the appropriate character-encoding "IO Layer" (see docs for PerlIO). Something like this:

    my $open_mode = "<:utf8"; # or "<:encoding(UTF-16LE)" etc. open( IN, $open_mode, "unicode_input.txt" ); binmode( STDOUT, ":utf8" ); # or whatever form of unicode is supported + by your display tool while (<IN>) { # data will be read as (or converted to) utf8 on input # do stuff with $_, then print; }
    Apart from that, the code you posted has a lot of unnecessary effort and temporary storage. You read into a "prehasha" array, then copy that into "char" and "pinyin" arrays, then finally try to put that into a hash (though it's not clear that you succeed there). You could just read into the hash. Following on the snippet above (where the file is being read as unicode data):
    my %charpinyin; while (<IN>) { chomp; my ( $chchar, $pinyin ) = split /\t/; $charpinyin{$chchar} = $pinyin; }
    (updated last snippet to reflect the OP's use of tab-delimited data)
A reply falls below the community's threshold of quality. You may see it by logging in.