Convert string number to a integer

I have data as below

"ROWS merge process complete.  thousand rows changed"

I need to get a variable assigned the value of 1000. I mean convert the string thousand to 1000.

Any help or pointer.

What if it were "one thousand and one"? And, which strange application / program would give an output like the quoted?

I recall to have seen an string - number converter in these forums some time (a year?) ago - try a search.

Yes, I need to convert that to 10001 if that is the case.

Well, in fact it was almost 2 years ago, and it was number to string. You might want to use this as a starting point and/or a source of brilliant ideas:

1 Like

Hi.

The reverse of that thread would be English to number.

A DIY start is Lingua::EN::Words2Nums - convert English text to numbers - metacpan.org

Good luck ... cheers, drl

---------- Post updated Jan 28th, 2017 at 08:23 ---------- Previous update was Jan 27th, 2017 at 11:55 ----------

Hi.

Here is a sample, adapted from the module documentation:

#!/usr/bin/env perl

# @(#) p2       Demonstrate conversion of English to numbers, perl module.

use strict;
use warnings;
use Lingua::EN::Words2Nums;

my ( $num, $e, $f );
print "\n";

$e = "two thousand and one";
$num = words2nums($e);
print " $e = $num\n\n";

$e   = "ROWS merge process complete.  thousand rows changed";
$num = words2nums($e);
print " $e = $num\n\n" if defined $num;

$e   = "twenty-second";
$num = words2nums($e);
print " $e = $num\n\n" if defined $num;

$e   = "15 billion, 6 million, and nineteen";
$num = words2nums($e);
$f   = commify($num) if defined $num;
print " $e = $num, commified = $f\n" if defined $num;

exit(0);

sub commify {
  my $number = shift;

  # 1 while $number =~ s/(\d+)(\d\d\d)/$1,$2/;
  1 while $number =~ s/^([-+]?\d+)(\d{3})/$1,$2/;
  return $number;
}

producing:

$ ./p2

 two thousand and one = 2001

 twenty-second = 22

 15 billion, 6 million, and nineteen = 15006000019, commified = 15,006,000,019

On a system:

OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.7 (jessie) 
perl 5.20.2

Note that the OP text ROWS merge process complete. thousand rows changed did not produce output, because there were text strings that were not understood by the module.

One would need to extract the strings of interests, possibly by parsing the line, collecting strings, testing sequences, and, finally accepting the conversion.

Good luck ... cheers, drl

1 Like

Excellent! One question: Wouldn't "Twenty-second" require a trailing ordinal point / dot?

1 Like

Hi, RudiC.

If I were doing the code, I think I'd add an option for suffixes, e.g. twenty-second -> 22nd for ordinals.

This code is available in repositories, as well as at Lingua::EN::Words2Nums - convert English text to numbers - metacpan.org . It seems to be about 10 years old, but may be amenable to updates. I also didn't dig very deeply -- this seemed to work -- but there may be other modules, perl, python, etc., that can do the same.

A brief look at the module seems like much of the work is table-driven, so it may be just a bit of work to add another table to optionally add suffixes.

There are always things of interest that come up in searches at the repo at cpan.org.

Best wishes ... cheers, drl