I'm trying to learn vietnamese, and I thought I'd write a little Perl script which would help me learn at least the numbers. Hit a key, get a number, have to translate it into english, or vice-versa.

Vietnamese numbers, with a couple of exceptions, are very very logical.

The number twelve is just (word-for-ten)(word-for-two) for instance. The number thirty is just (word-for-three)(word-for-ten) and the number thirty-two is just (word-for-three)(word-for-ten)(word-for-two).

The one main exception therefore is that numbers after twenty go two-ten-two, two-ten-three, etc, and the numbers between ten and twenty just go ten-one, ten-two, not one-ten-two.

However, despite the language being logical, I'm not so logical, obviously.

Here's what I came up with. Reverse the digits of the numbers, push the words, using a hash, onto an array, and then reverse the array and print it.

The code below works up to a hundred, but not after that.

I'm sure I'm missing some easy way to do this. Any help gratefully recieved.

%numbers = ( 0, '', 1, 'mot', 2, 'hai', 3, 'ba', 4, 'bon', 5, 'nam', 6, 'sau', 7, 'bay', 8, 'tam', 9, 'chin', 10, 'muoi', 100, 'tram', 1000, 'ngan' ); sub translate { my $number = $_[0]; my @output = (); if (defined($numbers{$number})) { $text_version = $numbers{$number}; print "$text_version \n"; } else { $reverse_number = reverse($number); $places = 1; @digits = split ('', $reverse_number); foreach $digit (@digits) { if ($places == 1) { push (@output, $numbers{$digit}); } else { push (@output, $numbers{$places}); unless ($number < 20) { push (@output, $numbers{$digit}); } } $places *= 10; } @output = reverse(@output); print "@output \n"; } }

--
($_='jjjuuusssttt annootthheer pppeeerrrlll haaaccckkeer')=~y/a-z//s;print;

In reply to Vietnamese Numbers by Cody Pendant

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.