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:
Format a Data File

 



adamjazz1
Novice

Nov 23, 2009, 10:44 AM

Post #1 of 6 (677 views)
Format a Data File Can't Post

Hi,

I am trying to change the format of a data file to match other data files format.


Code
This is a single entry for the data file I would like to change. 
3.2952 CA
3.5579 CA
3.1308 CA
4.9674 CA
4.3178 CA
3.7349 CA
2.7737 CA
3.7845 CA

I am trying to figure out how to get all of the numbers next to eachother and remove the "CA" like this:

3.2952 3.5579 3.1308 4.9674 4.3178 3.7349 2.7737 3.7845


Thanks for any input!


bichonfrise74
Novice

Nov 23, 2009, 11:19 AM

Post #2 of 6 (676 views)
Re: [adamjazz1] Format a Data File [In reply to] Can't Post

Try this.

Code
#!/usr/bin/perl 

use strict;

my @records;
while (<DATA>) {
my ($number) = /\s+(\d\.\d+)\s/;
push( @records, $number );
}

print join ' ', @records;

__DATA__
3.2952 CA
3.5579 CA
3.1308 CA
4.9674 CA
4.3178 CA
3.7349 CA
2.7737 CA
3.7845 CA



--bichonfrise74


adamjazz1
Novice

Nov 23, 2009, 12:15 PM

Post #3 of 6 (671 views)
Re: [bichonfrise74] Format a Data File [In reply to] Can't Post

bichonfrise74 - Thanks it works great, but...

How can I print the results with only 8 sets of numbers per line?

Code
3.9708 2.8774 3.7691 3.6411 5.6122 4.3220 3.7427 4.6749  3.8152 2.9114 3.8420 3.7026 5.7437 4.3256 3.7160 4.6640 
From this to:
3.9708 2.8774 3.7691 3.6411 5.6122 4.3220 3.7427 4.6749
3.8152 2.9114 3.8420 3.7026 5.7437 4.3256 3.7160 4.6640


Thank you very much for your help!


bichonfrise74
Novice

Nov 23, 2009, 12:30 PM

Post #4 of 6 (669 views)
Re: [adamjazz1] Format a Data File [In reply to] Can't Post

Add this.

Code
my $count; 
for my $i (0 .. $#records) {
print $records[$i] . ' ';
$count++;
if ( $count == 8 ) {
print "\n";
$count = 0;
}
}



--bichonfrise74


adamjazz1
Novice

Nov 23, 2009, 2:22 PM

Post #5 of 6 (666 views)
Re: [bichonfrise74] Format a Data File [In reply to] Can't Post

Almost there...... :)

3.9708 2.8774 3.7691 3.6411 5.6122 4.3220 3.7427 4.6749
3.8152 2.9114 3.8420 3.7026 5.7437 4.3256 3.7160
4.6640 3.8772 2.9369 3.8289 3.6608 5.7934 4.3226

Code
It is printing out like this. The first entry has 8 sets of numbers, but the rest has 7. 


ichi
User

Nov 23, 2009, 6:34 PM

Post #6 of 6 (656 views)
Re: [adamjazz1] Format a Data File [In reply to] Can't Post


Code
$\ = ' '; 
while (<>) {
($f1) = split ' ' ;
print $f1;
}



(This post was edited by ichi on Nov 23, 2009, 6:36 PM)

 
 


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

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