change a single digit to corresponding English word

Discussion in 'Perl Misc' started by April, Jun 24, 2008.

  1. April

    April Guest

    Will the following change a single digit to corresponding English
    word, like 3 to three, 5 to five, etc?

    $char = $numbers[$char]
    April, Jun 24, 2008
    #1
    1. Advertising

  2. April <> wrote:
    >Will the following change a single digit to corresponding English
    >word, like 3 to three, 5 to five, etc?
    >
    >$char = $numbers[$char]


    Assuming that the array @numbers contains the string 'three' at index 3,
    then yes, after this assignment $char will contain the string 'three'.
    In which case $char is a very poor choice for an identifier.

    jue
    Jürgen Exner, Jun 24, 2008
    #2
    1. Advertising

  3. April wrote:
    > Will the following change a single digit to corresponding English
    > word, like 3 to three, 5 to five, etc?
    >
    > $char = $numbers[$char]


    Only if the array @numbers, with those words, exists.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Jun 24, 2008
    #3
  4. April

    Jack D Guest

    "April" <> wrote in message
    news:...
    > Will the following change a single digit to corresponding English
    > word, like 3 to three, 5 to five, etc?
    >
    > $char = $numbers[$char]



    In addition to the answers already given, I thought I would also point out
    that there is a module that does this very thing:

    http://search.cpan.org/~sburke/Lingua-EN-Numbers-1.01/

    Jack
    Jack D, Jun 24, 2008
    #4
  5. April

    April Guest

    On Jun 23, 11:19 pm, Jürgen Exner <> wrote:
    > April <> wrote:
    > >Will the following change a single digit to corresponding English
    > >word, like 3 to three, 5 to five, etc?

    >
    > >$char = $numbers[$char]

    >
    > Assuming that the array @numbers contains the string 'three' at index 3,
    > then yes, after this assignment $char will contain the string 'three'.
    > In which case $char is a very poor choice for an identifier.
    >
    > jue


    Thanks jue and everyone .. I just stumble on this and would like to
    know how this "$char = $numbers[$char]" accually work ... here is the
    entire program:

    #!/usr/bin/perl -w

    use strict;

    my @numbers = qw(zero one two three four five six seven eight nine);

    print "Enter a string containing a number: ";
    chomp(my $answer = <STDIN>);

    my @characters = split ("", $answer);

    foreach my $char (@characters)
    {

    $char = $numbers[$char]
    if ($char ge "0" && $char le "9");
    }

    print "The output is: @characters\n";
    April, Jun 24, 2008
    #5
  6. April

    Ted Zlatanov Guest

    On Tue, 24 Jun 2008 00:42:09 -0600 "Jack D" <> wrote:

    JD> "April" <> wrote in message
    JD> news:...
    >> Will the following change a single digit to corresponding English
    >> word, like 3 to three, 5 to five, etc?
    >>
    >> $char = $numbers[$char]


    JD> In addition to the answers already given, I thought I would also point
    JD> out that there is a module that does this very thing:

    JD> http://search.cpan.org/~sburke/Lingua-EN-Numbers-1.01/

    I wonder if there's a locale-sensitive way to say numbers from inside
    Perl. It must be available (part of glibc I think), since I know `date`
    for example can print the day correctly depending on the locale.

    I didn't see anything in CPAN, only
    http://search.cpan.org/~rgarcia/perl-5.10.0/ext/I18N/Langinfo/Langinfo.pm

    The CPAN *Locale* modules were not very useful for this task.

    Lingua::Any::Numbers doesn't sniff out the locale automatically, and
    doesn't seem to use the glibc facilities (if such exist).

    Ted
    Ted Zlatanov, Jun 24, 2008
    #6
  7. April

    Ben Bullock Guest

    On Tue, 24 Jun 2008 08:09:18 -0500, Ted Zlatanov wrote:

    > On Tue, 24 Jun 2008 00:42:09 -0600 "Jack D" <> wrote:


    > JD> In addition to the answers already given, I thought I would also point
    > JD> out that there is a module that does this very thing:
    >
    > JD> http://search.cpan.org/~sburke/Lingua-EN-Numbers-1.01/


    This is rather a handy module by the way.

    > I wonder if there's a locale-sensitive way to say numbers from inside
    > Perl. It must be available (part of glibc I think), since I know `date`
    > for example can print the day correctly depending on the locale.


    Dates are a different issue from numbers. I'm fairly sure glibc has
    nothing as fancy as this. But there are some modules which deal with
    numbers, like Lingua::JA::Numbers, which you can see in action on my
    website:

    http://convert.sljfaq.org/numbers.cgi

    Generally, converting numbers into a string of text is rather a
    difficult problem requiring a lot of knowledge of the language you
    want to write the number in. It's not something which one would expect
    to find in a C library.

    > I didn't see anything in CPAN, only
    > http://search.cpan.org/~rgarcia/perl-5.10.0/ext/I18N/Langinfo/Langinfo.pm
    >
    > The CPAN *Locale* modules were not very useful for this task.
    >
    > Lingua::Any::Numbers doesn't sniff out the locale automatically, and
    > doesn't seem to use the glibc facilities (if such exist).


    Lingua::Any::Numbers seems to be nothing but a wrapper for various
    other Perl modules, like the above-mentioned Lingua::JA::Numbers.
    Ben Bullock, Jun 24, 2008
    #7
  8. April wrote:
    > Thanks jue and everyone .. I just stumble on this and would like to
    > know how this "$char = $numbers[$char]" accually work ... here is the
    > entire program:
    >
    > #!/usr/bin/perl -w
    >
    > use strict;
    >
    > my @numbers = qw(zero one two three four five six seven eight nine);
    >
    > print "Enter a string containing a number: ";
    > chomp(my $answer = <STDIN>);
    >
    > my @characters = split ("", $answer);
    >
    > foreach my $char (@characters)
    > {
    >
    > $char = $numbers[$char]
    > if ($char ge "0" && $char le "9");
    > }
    >
    > print "The output is: @characters\n";


    I suggest that you do some reading first.

    perldoc perlintro

    perldoc perldata

    If you don't understand the above code after having studied those parts
    of the docs, then come back and ask a more specific question.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Jun 24, 2008
    #8
  9. April <> wrote:
    > I just stumble on this and would like to
    >know how this "$char = $numbers[$char]" accually work


    Do you know what an array is and how to access its elements?

    jue
    Jürgen Exner, Jun 24, 2008
    #9
  10. April

    Ted Zlatanov Guest

    number to word in any language (was: change a single digit to corresponding English word)

    On Tue, 24 Jun 2008 13:26:54 +0000 (UTC) Ben Bullock <> wrote:

    BB> On Tue, 24 Jun 2008 08:09:18 -0500, Ted Zlatanov wrote:
    >> I wonder if there's a locale-sensitive way to say numbers from inside
    >> Perl. It must be available (part of glibc I think), since I know `date`
    >> for example can print the day correctly depending on the locale.


    BB> Dates are a different issue from numbers. I'm fairly sure glibc has
    BB> nothing as fancy as this. But there are some modules which deal with
    BB> numbers, like Lingua::JA::Numbers

    Right, thanks.

    BB> Generally, converting numbers into a string of text is rather a
    BB> difficult problem requiring a lot of knowledge of the language you
    BB> want to write the number in. It's not something which one would expect
    BB> to find in a C library.

    It's not terribly hard to do in the languages I know (some are covered
    by Lingua::*::Numbers). Inflecting the numbers is hard, but you can do
    a perfectly passable job if you assume a "one item, two items..."
    sentence structure. I'd rather have an ugly solution than none. Ugly
    solutions tend to get fixed. Anyhow, I checked the /usr/lib/locale
    stuff and it doesn't seem to have any of this functionality so I guess
    it has to happen at the Perl level.

    >> I didn't see anything in CPAN, only
    >> http://search.cpan.org/~rgarcia/perl-5.10.0/ext/I18N/Langinfo/Langinfo.pm
    >>
    >> The CPAN *Locale* modules were not very useful for this task.
    >>
    >> Lingua::Any::Numbers doesn't sniff out the locale automatically, and
    >> doesn't seem to use the glibc facilities (if such exist).


    BB> Lingua::Any::Numbers seems to be nothing but a wrapper for various
    BB> other Perl modules, like the above-mentioned Lingua::JA::Numbers.

    I understand, but my point was that it doesn't figure out what you want
    based on the locale. For example:

    % LANG=bg_BG.utf8 date
    вт юни 24 14:41:09 CDT 2008
    % LANG=C date
    Tue Jun 24 14:41:12 CDT 2008

    I don't know how to map any locale to any language in the
    Lingua::*::Numbers hierarchy. In addition, the .utf8* specifier is a
    problem (see 'sr_YU.utf8@cyrillic' for example). The Lingua::*::Numbers
    modules are not setting the output encoding; it seems like that's up to
    the application. So Lingua::Any::Numbers should have wrappers to
    accomodate encodings and auto-detect locale. If anyone knows more on
    this topic please let me know, before I go writing patches. I cc-ed
    Burak, the Lingua::Any::Numbers maintainer on this and will report back
    if he e-mails me.

    Ted
    Ted Zlatanov, Jun 24, 2008
    #10
  11. April

    Ben Bullock Guest

    Re: number to word in any language

    Ted Zlatanov <> wrote:

    > The Lingua::*::Numbers
    > modules are not setting the output encoding; it seems like that's up to
    > the application.


    Quibble: there is no "the" output encoding in Perl. It's possible to
    set an output encoding for each stream (e.g. open a file and set its
    output as UTF-8, and set STDOUT as something else, and print the same
    string to both, and have it come out in the correct encoding), so
    unless the modules actually write output (which I doubt) it doesn't
    make sense for them to set an output encoding.

    If you mean the encoding of the strings themselves, that should be
    done in Perl's internal encoding. I know Lingua::JA::Numbers does that
    (the author is actually the maintainer of Encode.pm, so we'd expect
    that much from him), but I have no idea about the rest of them.
    Ben Bullock, Jun 25, 2008
    #11
  12. April

    Ted Zlatanov Guest

    Re: number to word in any language

    On Wed, 25 Jun 2008 06:35:32 +0000 (UTC) (Ben Bullock) wrote:

    BB> Ted Zlatanov <> wrote:
    >> The Lingua::*::Numbers
    >> modules are not setting the output encoding; it seems like that's up to
    >> the application.


    BB> Quibble: there is no "the" output encoding in Perl. It's possible to
    BB> set an output encoding for each stream (e.g. open a file and set its
    BB> output as UTF-8, and set STDOUT as something else, and print the same
    BB> string to both, and have it come out in the correct encoding), so
    BB> unless the modules actually write output (which I doubt) it doesn't
    BB> make sense for them to set an output encoding.

    I'm saying that language locales (the ones with glibc at least) include
    the output encoding in the specification of some locales. It seems hard
    to DTRT with locales in a Perl module because of this. Perhaps all
    strings emitted by the module should be converted to that encoding
    explicitly.

    Ted
    Ted Zlatanov, Jun 26, 2008
    #12
  13. April

    Henry Law Guest

    Gunnar Hjalmarsson wrote:
    > April wrote:


    >> my @characters = split ("", $answer);
    >>
    >> foreach my $char (@characters)
    >> {
    >>
    >> $char = $numbers[$char]
    >> if ($char ge "0" && $char le "9");
    >> }
    >>
    >> print "The output is: @characters\n";

    >
    > I suggest that you do some reading first.
    >
    > perldoc perlintro
    >
    > perldoc perldata


    Unfair, Gunnar: the key text is in perlsyn:

    .... the foreach loop index variable is an implicit alias for each item
    in the list that you're looping over.

    But I didn't know that so the code has improved my education.

    --

    Henry Law Manchester, England
    Henry Law, Jun 28, 2008
    #13
  14. Henry Law wrote:
    > Gunnar Hjalmarsson wrote:
    >> April wrote:
    >>>
    >>> my @characters = split ("", $answer);
    >>>
    >>> foreach my $char (@characters)
    >>> {
    >>>
    >>> $char = $numbers[$char]
    >>> if ($char ge "0" && $char le "9");
    >>> }
    >>>
    >>> print "The output is: @characters\n";

    >>
    >> I suggest that you do some reading first.
    >>
    >> perldoc perlintro
    >>
    >> perldoc perldata

    >
    > Unfair, Gunnar:


    What has fairness to do with it?

    > the key text is in perlsyn:
    >
    > ... the foreach loop index variable is an implicit alias for each item
    > in the list that you're looping over.


    The OP seemed to wonder about accessing individual elements in an array,
    specifically the array @numbers (which you chose to not quote). Hence my
    pointers to perlintro and perldata.

    > But I didn't know that so the code has improved my education.


    I don't mind if both you and the OP read perlsyn as well. :)

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Jun 28, 2008
    #14
  15. Re: number to word in any language (was: change a single digit tocorresponding English word)

    On 2008-06-24 19:52, Ted Zlatanov <> wrote:
    > On Tue, 24 Jun 2008 13:26:54 +0000 (UTC) Ben Bullock <> wrote:
    >
    > BB> On Tue, 24 Jun 2008 08:09:18 -0500, Ted Zlatanov wrote:
    >>> I didn't see anything in CPAN, only
    >>> http://search.cpan.org/~rgarcia/perl-5.10.0/ext/I18N/Langinfo/Langinfo.pm
    >>>
    >>> The CPAN *Locale* modules were not very useful for this task.
    >>>
    >>> Lingua::Any::Numbers doesn't sniff out the locale automatically, and
    >>> doesn't seem to use the glibc facilities (if such exist).

    >
    > BB> Lingua::Any::Numbers seems to be nothing but a wrapper for various
    > BB> other Perl modules, like the above-mentioned Lingua::JA::Numbers.
    >
    > I understand, but my point was that it doesn't figure out what you want
    > based on the locale. For example:
    >
    > % LANG=bg_BG.utf8 date
    > вт юни 24 14:41:09 CDT 2008
    > % LANG=C date
    > Tue Jun 24 14:41:12 CDT 2008
    >
    > I don't know how to map any locale to any language in the
    > Lingua::*::Numbers hierarchy.


    The best you can do is check that the locale matches
    /^([a-z]{2})($|_[A-Z]{2}\b)/ and check if
    "Lingua::\U$1\E::Numbers" exists. you won't find Lingua::Slavic::Numbers
    that way, and not all locales start with a language code, but there
    doesn't seem any more dependable way to extract the language code from
    the locale than to rely on the naming convention.


    > In addition, the .utf8* specifier is a
    > problem (see 'sr_YU.utf8@cyrillic' for example). The Lingua::*::Numbers
    > modules are not setting the output encoding; it seems like that's up to
    > the application.


    You can get the encoding from the current locale with:

    use I18N::Langinfo qw(langinfo CODESET)
    $charset = langinfo(CODESET)


    > So Lingua::Any::Numbers should have wrappers to
    > accomodate encodings and auto-detect locale.


    I've never used Lingua::Any::Numbers, so I don't care much about it but
    I think that would be a mistake. Encodings are strictly for I/O, any
    module dealing with strings should just use character strings. Getting
    the language code from the locale could be useful, but then it would be
    more useful as an independent module (or possibly as an extension to
    I18N::Langinfo).

    hp
    Peter J. Holzer, Jun 29, 2008
    #15
  16. April

    Ted Zlatanov Guest

    Lingua::Slavic::Numbers (was: number to word in any language)

    On Sun, 29 Jun 2008 22:38:11 +0200 "Peter J. Holzer" <> wrote:

    PJH> The best you can do is check that the locale matches
    PJH> /^([a-z]{2})($|_[A-Z]{2}\b)/ and check if
    PJH> "Lingua::\U$1\E::Numbers" exists. you won't find Lingua::Slavic::Numbers
    PJH> that way, and not all locales start with a language code, but there
    PJH> doesn't seem any more dependable way to extract the language code from
    PJH> the locale than to rely on the naming convention.

    Since I put Lingua::Slavic::Numbers up, I intend to also provide
    wrappers (Lingua::LL::Numbers) for it in the same distribution. I just
    haven't gotten around to it.

    If anyone is interested in testing the existing Bulgarian translations,
    please do. In addition, if you can provide other test cases for other
    Slavic languages (Russian is probably the big one, but others would be
    good too: Macedonian, Serbian, Slovak, etc.), I would appreciate it. My
    knowledge is not good enough to write the test cases but I can get going
    once I have those.

    Ted
    Ted Zlatanov, Jun 30, 2008
    #16
  17. April

    Ted Zlatanov Guest

    Re: Lingua::Slavic::Numbers

    On Mon, 7 Jul 2008 22:14:32 +0300 Burak Gürsoy <> wrote:

    BG> I've just released 0.21 which has locale support (although it can be a
    BG> little experimental)

    BG> use Lingua::Any::Numbers 0.21 qw:)std +locale);
    BG> print to_string(45);

    BG> or

    BG> use Lingua::Any::Numbers 0.21 qw:)std);
    BG> print to_string(45,'locale');

    BG> however, you still have to manually install other Lingua::*::Numbers (other
    BG> than "EN") to get a meaningful interface.

    Thank you, I'm reporting this back to comp.lang.perl.misc as promised.

    >> % LANG=bg_BG.utf8 date
    >> вт юни 24 14:41:09 CDT 2008


    BG> Also, the overall interface will be limited with the availability of the
    BG> language specific module. For example, there is no such module named
    BG> Lingua::BG::Numbers AFAIK.

    I'm trying to bundle Lingua::BG::Numbers with Lingua::Slavic::Numbers
    and it's not working. I asked on comp.lang.perl.modules but haven't had
    any luck yet. The module is written, though.

    Thanks
    Ted
    Ted Zlatanov, Jul 7, 2008
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fangs
    Replies:
    3
    Views:
    9,793
    darshana
    Oct 26, 2008
  2. =?Utf-8?B?UmFlZCBTYXdhbGhh?=

    English/English DLL

    =?Utf-8?B?UmFlZCBTYXdhbGhh?=, Oct 15, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    1,674
    =?Utf-8?B?UmFlZCBTYXdhbGhh?=
    Oct 16, 2005
  3. IchBin
    Replies:
    1
    Views:
    774
  4. Replies:
    2
    Views:
    954
    Martin Honnen
    Apr 12, 2008
  5. draq
    Replies:
    10
    Views:
    187
    Christer Nilsson
    Dec 13, 2005
Loading...

Share This Page