change a single digit to corresponding English word

A

April

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

$char = $numbers[$char]
 
J

Jürgen Exner

April said:
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
 
G

Gunnar Hjalmarsson

April said:
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.
 
A

April

April said:
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";
 
T

Ted Zlatanov

JD> "April said:
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
 
B

Ben Bullock

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.
 
G

Gunnar Hjalmarsson

April said:
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.
 
J

Jürgen Exner

April said:
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
 
T

Ted Zlatanov

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.

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
 
B

Ben Bullock

Ted Zlatanov said:
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.
 
T

Ted Zlatanov

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
 
H

Henry Law

Gunnar said:
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.
 
G

Gunnar Hjalmarsson

Henry said:
Gunnar said:
April said:
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. :)
 
P

Peter J. Holzer

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
 
T

Ted Zlatanov

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
 
T

Ted Zlatanov

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.

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top