Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Best way to extract numeric values from a report?

Reply
Thread Tools

Best way to extract numeric values from a report?

 
 
bobmct
Guest
Posts: n/a
 
      05-07-2009
I'm reasonably familiar with perl as I have been programming with it
for a number of years.

I am trying to extract information from a text based report file and
create a file of data records for use as input to some other perl
programs.

The report file in question has about 13 columns of financial amounts
such as -1,234,567.89 and I wish to extract each column and output to
a record in a signed, efficient format for later use by these other
perl programs.

I am currently using unpack and pack but I am not having luck with the
floating point values.

What are some suggestions as to how to extract and export these
values? Any good suggestions/recommendations/examples greatly
appreciated.

signed: just another perl monger

 
Reply With Quote
 
 
 
 
J. Gleixner
Guest
Posts: n/a
 
      05-07-2009
bobmct wrote:
> I'm reasonably familiar with perl as I have been programming with it
> for a number of years.
>
> I am trying to extract information from a text based report file and
> create a file of data records for use as input to some other perl
> programs.
>
> The report file in question has about 13 columns of financial amounts
> such as -1,234,567.89 and I wish to extract each column and output to
> a record in a signed, efficient format for later use by these other
> perl programs.


What separates the columns?

Use whatever that is, with split, and you should be on your way.

>
> I am currently using unpack and pack but I am not having luck with the
> floating point values.


Show us what you've tried.

>
> What are some suggestions as to how to extract and export these
> values? Any good suggestions/recommendations/examples greatly
> appreciated.



Treating it as a string, using a regular expression, would
be much easier than pack, IMHO.
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      05-08-2009
bobmct <(E-Mail Removed)> wrote:
>I am trying to extract information from a text based report file and
>create a file of data records for use as input to some other perl
>programs.
>
>The report file in question has about 13 columns of financial amounts
>such as -1,234,567.89 and I wish to extract each column and output to
>a record in a signed, efficient format for later use by these other
>perl programs.


How are the columns identified/separated? By fixed position within the
line (i.e. characters 12-18 are always column 4) or by a known
separator, e.g. a comma, a semicolon, or a space character?

If the former then unpack is the tool of choice, if the latter then
split() will work nicely.
And if it's a CSV-format then one of the CSV-modules will work best,
because they will deal with all the oddities like quoted values or
escaped characters automatically.

jue
 
Reply With Quote
 
ccc31807
Guest
Posts: n/a
 
      05-08-2009
On May 7, 5:48*pm, bobmct <(E-Mail Removed)> wrote:
> I am trying to extract information from a text based report file and
> create a file of data records for use as input to some other perl
> programs.


First, I assume that you are dealing with some kind of delimited
format. The algorithm is easy -- here is some pseudo-pseudo code:

open INFILE, '<', 'text_based_report_file.txt';
while(<INFILE>)
{
chomp;
my ($c1, $c2, etc. all the way to $c13) = split /delimiter/, $_;
#you can now access your values like $c1, $c2, ... $c13)
}
close INFILE;

> The report file in question has about 13 columns of financial amounts
> such as -1,234,567.89 and I wish to extract each column and output to
> a record in a signed, efficient format for later use by these other
> perl programs.


It's not clear to me exactly how you want your output, but assuming
that it's a signed float and all you need to do is delete the commas,
this will do it:
$c1 =~ s/,//g;

> What are some suggestions as to how to extract and export these
> values? *Any good suggestions/recommendations/examples greatly
> appreciated.


How do you want to export the values? Seems to me that if you input
them in a delimited file your work is done, that you don't need to
export the values. If you need to 'export' the values into some data
structure in memory, use an array of array references. You can then
access each individual value like this:
$arr_ref->[0][0] #gets the first column in the first row
$arr_ref->[1][12] # gets the last column in the second row

CC

 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      05-08-2009
On 2009-05-08 00:23, ccc31807 <(E-Mail Removed)> wrote:
> On May 7, 5:48*pm, bobmct <(E-Mail Removed)> wrote:
>> The report file in question has about 13 columns of financial amounts
>> such as -1,234,567.89 and I wish to extract each column and output to
>> a record in a signed, efficient format for later use by these other
>> perl programs.

>
> It's not clear to me exactly how you want your output, but assuming
> that it's a signed float and all you need to do is delete the commas,
> this will do it:
> $c1 =~ s/,//g;


For monetary values, you often want to store the amount in the smallest
denomination. So -1,234,567.89 should be stored as -123456789 cents, not
as -1234567.89 Dollars. The reason for this is that decimal fractions
cannot be represented exactly in binary, so the amount is rounded to the
nearest multiple of a power of two (which happens to be
-1234567.88999999989755451679229736328125 in this case).

If the amounts are always given with two digits after the decimal point,
you can just strip the decimal point, too:

$c1 =~ s/[,.]//g;
or
$c1 =~ tr/.,//d;

If the number of digits after the decimal point varies, you need to
normalize them first. Be careful!

hp
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extract the numeric and alphabetic part from an alphanumeric string Sandhya Prabhakaran Python 6 08-03-2009 04:40 PM
Extract Numeric values from string Vishal G Perl Misc 14 09-12-2008 03:36 PM
int to numeric numeric(18,2) ? jobs ASP .Net 2 07-22-2007 12:32 AM
Arithmetic overflow error converting numeric to data type numeric. darrel ASP .Net 4 07-19-2007 09:57 PM
check if string contains numeric, and check string length of numeric value ief@specialfruit.be C++ 5 06-30-2005 01:08 PM



Advertisments