Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   change a single digit to corresponding English word (http://www.velocityreviews.com/forums/t907618-change-a-single-digit-to-corresponding-english-word.html)

April 06-24-2008 03:06 AM

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

$char = $numbers[$char]

Jürgen Exner 06-24-2008 03:19 AM

Re: change a single digit to corresponding English word
 
April <xiaoxia2005a@yahoo.com> 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

Gunnar Hjalmarsson 06-24-2008 03:26 AM

Re: change a single digit to corresponding English word
 
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

Jack D 06-24-2008 06:42 AM

Re: change a single digit to corresponding English word
 
"April" <xiaoxia2005a@yahoo.com> wrote in message
news:3f8e4157-755b-426e-92a5-0d0fe7b241b6@k13g2000hse.googlegroups.com...
> 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



April 06-24-2008 11:51 AM

Re: change a single digit to corresponding English word
 
On Jun 23, 11:19*pm, Jürgen Exner <jurge...@hotmail.com> wrote:
> April <xiaoxia20...@yahoo.com> 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";


Ted Zlatanov 06-24-2008 01:09 PM

Re: change a single digit to corresponding English word
 
On Tue, 24 Jun 2008 00:42:09 -0600 "Jack D" <jack@home.net> wrote:

JD> "April" <xiaoxia2005a@yahoo.com> wrote in message
JD> news:3f8e4157-755b-426e-92a5-0d0fe7b241b6@k13g2000hse.googlegroups.com...
>> 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...fo/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

Ben Bullock 06-24-2008 01:26 PM

Re: change a single digit to corresponding English word
 
On Tue, 24 Jun 2008 08:09:18 -0500, Ted Zlatanov wrote:

> On Tue, 24 Jun 2008 00:42:09 -0600 "Jack D" <jack@home.net> 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...fo/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.



Gunnar Hjalmarsson 06-24-2008 01:35 PM

Re: change a single digit to corresponding English word
 
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

Jürgen Exner 06-24-2008 01:43 PM

Re: change a single digit to corresponding English word
 
April <xiaoxia2005a@yahoo.com> 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

Ted Zlatanov 06-24-2008 07:52 PM

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 <benkasminbullock@gmail.com> 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...fo/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


All times are GMT. The time now is 06:55 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.