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: Beginner:
HELP!! Multiplying numbers in a file using Perl.

 



libertyforall
Novice

Sep 20, 2010, 3:29 PM

Post #1 of 15 (1270 views)
HELP!! Multiplying numbers in a file using Perl. Can't Post

I need to multiply all numbers without a / or beginning with S by 4,285.4

If my file name is file1 and I want to modify the file as a stated above, how would I write the script? I am a new newbie! Here is an example of what the text on the file will look like.

JDAY YR MO DA1 HR1 DA2 HR2 S00000037 S00000021 S00000002 S00000004 S00000034 S00000035 S00000075 S00000038 S00000039 S00000040 S00000041 S00000042 S00000043 S00000044 S00000045 S00000046
09/13/2010 10 9 13 12 13 13 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
09/13/2010 10 9 13 13 13 14 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
09/13/2010 10 9 13 14 13 15 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
09/13/2010 10 9 13 15 13 16 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
09/13/2010 10 9 13 16 13 17 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
09/13/2010 10 9 13 17 13 18 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
09/13/2010 10 9 13 18 13 19 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
09/13/2010 10 9 13 19 13 20 0. 0. 0. 0. 0.156 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
09/13/2010 10 9 13 20 13 21 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
09/13/2010 10 9 13 21 13 22 0. 0. 0. 0. 0.022 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
09/13/2010 10 9 13 22 13 23 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
09/13/2010 10 9 13 23 14 0 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
09/14/2010 10 9 14 0 14 1 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.


BillKSmith
Veteran

Sep 20, 2010, 8:27 PM

Post #2 of 15 (1264 views)
Re: [libertyforall] HELP!! Multiplying numbers in a file using Perl. [In reply to] Can't Post


Code
perl -p -e"s/([^S\/])(\d+\.?\d*)/$1.$2*4285.4/eg" yourfile.txt

Good Luck,
Bill


7stud
Enthusiast

Sep 20, 2010, 9:00 PM

Post #3 of 15 (1262 views)
Re: [libertyforall] HELP!! Multiplying numbers in a file using Perl. [In reply to] Can't Post


Quote
If my file name is file1 and I want to modify the file as a stated above


Generally, you are not going to modify a file. Your mindset should be: "How do I read a file and then write the output to a new file. If you need the new file to have the same name as the original file, then after you are done writing to the new file, delete the original file and rename the new file to the original file's name.


(This post was edited by 7stud on Sep 20, 2010, 9:02 PM)


libertyforall
Novice

Sep 21, 2010, 1:20 PM

Post #4 of 15 (1252 views)
Re: [BillKSmith] HELP!! Multiplying numbers in a file using Perl. [In reply to] Can't Post

When I use the script below either in the command line or as a perl script I get the error message at the bottom.

Code
#!/usr/local/bin/perl 

perl -p -e"s/([^S\/])(\d+\.?\d*)/$1.$2*4285.4/eg" HYSPLIT_1312.txt



Bareword found where operator expected at numbers.pl line 3, near ""s/([^S\/])(\d+\.?\d*)/$1.$2*4285.4/eg" HYSPLIT_1312"
(Missing operator before HYSPLIT_1312?)
syntax error at numbers.pl line 3, near "perl -p "
Execution of numbers.pl aborted due to compilation errors.


libertyforall
Novice

Sep 21, 2010, 1:21 PM

Post #5 of 15 (1250 views)
Re: [libertyforall] HELP!! Multiplying numbers in a file using Perl. [In reply to] Can't Post

I am also using perl v5.8.8


7stud
Enthusiast

Sep 21, 2010, 1:23 PM

Post #6 of 15 (1249 views)
Re: [libertyforall] HELP!! Multiplying numbers in a file using Perl. [In reply to] Can't Post

Don't try to cram a perl program into a command on the command line. Write a perl program in a separate file, and make the program do what you want based on what you have learned about the perl language.


libertyforall
Novice

Sep 21, 2010, 1:25 PM

Post #7 of 15 (1247 views)
Re: [7stud] HELP!! Multiplying numbers in a file using Perl. [In reply to] Can't Post

I tried both.


7stud
Enthusiast

Sep 21, 2010, 2:02 PM

Post #8 of 15 (1244 views)
Re: [libertyforall] HELP!! Multiplying numbers in a file using Perl. [In reply to] Can't Post

Post your program and any error messages you are getting. And copying that command line command into a separate file is not a perl program. A perl program would have an open() statement that opens the file and then a while loop that reads from the file as well as other program statements.


(This post was edited by 7stud on Sep 21, 2010, 2:03 PM)


libertyforall
Novice

Sep 21, 2010, 2:59 PM

Post #9 of 15 (1240 views)
Re: [7stud] HELP!! Multiplying numbers in a file using Perl. [In reply to] Can't Post

OK. Can you show me how to turn

Code
perl -p -e"s/([^S\/])(\d+\.?\d*)/$1.$2*4285.4/eg" HYSPLIT_1312.txt

Into a program?


BillKSmith
Veteran

Sep 21, 2010, 3:44 PM

Post #10 of 15 (1238 views)
Re: [libertyforall] HELP!! Multiplying numbers in a file using Perl. [In reply to] Can't Post

This works for me with perl 5.8.8 under windows XP. Try using single quotes rather than double.

If you wish to try it as a file, everyting within the quotes is a single perl statement. It consists only only of a single substitution operator. The /eg are options for that operator (refer perldoc perlop) The -p is a commandline option for perl (refer perldoc perlrun). Put it either on your command line or on the #! line.
Good Luck,
Bill


libertyforall
Novice

Sep 22, 2010, 12:23 PM

Post #11 of 15 (1232 views)
Re: [BillKSmith] HELP!! Multiplying numbers in a file using Perl. [In reply to] Can't Post

Ok. The single quotes seemed to work; however, it appears the data was dumped onto the terminal instead of modifying the original file. Ideally, it would either modify the original file or create a seperate file. I will be attaching the command as part of a larger script that runs automatically twice daily and will need it to dump the data in a specific file or the existing file. The other thing is that I just realized it cannot multiply all the number but only the numbers that occur after HR 2. This would be the numbers AFTER the first 7 numbers within each row of data.


JDAY YR MO DA1 HR1 DA2 HR2 S00000037 S00000021 S00000002 S00000004 S00000034 S00000035 S00000075 S00000038 S00000039 S00000040 S00000041 S00000042 S00000043 S00000044 S00000045 S00000046
09/13/2010 10 9 13 12 13 13 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
09/13/2010 10 9 13 13 13 14 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
09/13/2010 10 9 13 14 13 15 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
09/13/2010 10 9 13 15 13 16 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
09/13/2010 10 9 13 16 13 17 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
09/13/2010 10 9 13 17 13 18 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.


BillKSmith
Veteran

Sep 22, 2010, 1:25 PM

Post #12 of 15 (1228 views)
Re: [libertyforall] HELP!! Multiplying numbers in a file using Perl. [In reply to] Can't Post

Did you read the perldoc perlrun document? Did you try the -i option?

I now agree with 7Stud. Meeting your new requirements will take more than you can reasonably do in a single-line command. Implement and test this much as a file and then try to add to it. Show us what you have done.
Good Luck,
Bill


libertyforall
Novice

Sep 22, 2010, 2:03 PM

Post #13 of 15 (1226 views)
Re: [BillKSmith] HELP!! Multiplying numbers in a file using Perl. [In reply to] Can't Post

Ok. The -i option worked. I did notice another problem now. It appears that numbers that are in scientific notation are not multiplied correctly. It appeared to multiply numbers on both sides and the result looked like this 3719.7272E-12856.2 Numbers that were not multiplied in scientific notation were fine. So now, I just need to figure out how to write the code in a script, modify only the numbers in the rows starting from after the 7th number and make sure numbers in scientific notation, i.e. 1.2E-04 are multiplied correctly.


libertyforall
Novice

Sep 23, 2010, 12:31 PM

Post #14 of 15 (1211 views)
Re: [libertyforall] HELP!! Multiplying numbers in a file using Perl. [In reply to] Can't Post

Any ideas?


BillKSmith
Veteran

Sep 23, 2010, 4:55 PM

Post #15 of 15 (1204 views)
Re: [libertyforall] HELP!! Multiplying numbers in a file using Perl. [In reply to] Can't Post

It appears that you input file is not an arbitrary text file but rather a csv file. The fields are separated by white space (probably tab characters). The first line is the header line which should be copied not processed. Fields 1 through 7 contain time and date info. Fields 8 through 23 contain real data. Our job is to fix the units of this data.


Code
#!perl -pi.bak 
BEGIN{
use strict;
use warnings;
our $first = 1;
}

if (!$first) {
chomp;
my @fields = split;
@fields[7..22] = map {$_*4285.4} @fields[7..22];
$_ = join "\t", @fields;
$_ .= "\n";
}
$first = 0;

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