CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Check if txt file contains numbers

 



yasdel
Novice

Sep 19, 2010, 10:25 AM

Post #1 of 4 (574 views)
Check if txt file contains numbers Can't Post

Hello.

I wanna write a Perl script that searches into a .txt file and for each number it finds, applies a certain conversion to that :

Let say you have a txt file that contains such information inside :

0 700000 HR 700000 1300000 H3 1300000 2100000 H2 2100000 3800000 HH 3800000 4500000 HQ 4500000 5000000 HP 5000000 5700000 HO

Now, I want to multiply all the numbers found by a fix number (e.g r=1.5). So for example the first value after conversion will be :

0*1.5=0;

The second :

700000*1.5=1050000

HR will not be changed and so on. How can I do this please?

Notes : the numbers are real numbers (not necessarily integer)
Please in you code provide txt parsing as well.

thank you very much.

Best Regards,
YAS


BillKSmith
Veteran

Sep 19, 2010, 2:41 PM

Post #2 of 4 (555 views)
Re: [yasdel] Check if txt file contains numbers [In reply to] Can't Post

I recommend using the function 'is_numeric' in the CPAN module Data::Validate to select the numbers for processing.



Code
 use strict;   
use warnings;
use Data::Validate qw(:math);
foreach my $line (<DATA>) {
chomp ($line);
my @nums = split ' ', $line;
foreach my $num (@nums) {
$num *= 1.5 if is_numeric($num);
}
print "@nums\n";
}
__END__
0 700000 HR 700000 1300000 H3 1300000 2100000 H2 2100000 3800000 HH 3800000 4500000
HQ 4500000 5000000 HP 5000000 5700000 HO



Edit: Simplified processing.
Good Luck,
Bill

(This post was edited by BillKSmith on Sep 20, 2010, 5:57 AM)


yasdel
Novice

Sep 20, 2010, 5:39 AM

Post #3 of 4 (545 views)
Re: [BillKSmith] Check if txt file contains numbers [In reply to] Can't Post

Thanks for your solution.

I receive this error when running the code :


Code
Can't locate Data/Validate.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.10.0 /usr/local/share/perl/5.10.0 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.10 /usr/share/perl/5.10 /usr/local/lib/site_perl .) at cnvrt.pl line 4. 
BEGIN failed--compilation aborted at cnvrt.pl line 4.


I tried to install cpan module Data:validate but still I get the sale error; How cpan I solve this problem please? Thanks in advance


Code
perl -MCPAN -e shell 
install Data::Validate



BillKSmith
Veteran

Sep 20, 2010, 6:17 AM

Post #4 of 4 (537 views)
Re: [yasdel] Check if txt file contains numbers [In reply to] Can't Post

I cannot help with your CPAN problem, but I can suggest another solution to your original problem.

Assume that all your numbers are all positive integers. Then you could use the one-liner


Code
perl -pi.bak -e"/(\d+)/$1*1.5/eg" your_data.txt



In practice, you would have to replace '\d+' with a regular expression that would match all your numbers and nothing else. This is probably not practical if you must support all valid number formats.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives