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:
Stuck...Need some help

 



Okeanos
New User

Jul 5, 2009, 8:50 PM

Post #1 of 7 (327 views)
Stuck...Need some help Can't Post

I have a long list of txt files which all contain a list of numbers. I'm trying to write a script that takes the list of numbers out of each text file, and puts them into another txt file which would be tab delimited...

I have about 1000 of these files all which contain different data...they are listed in a directory as em1d_ip1.txt, em1d_ip2.txt,em1d_ip3.txt,....

em1d_ip.txt

2 0 3.86987e-005 02 0
1.6 0 5.89503e-005 0
1.4 0 7.45694e-005 0
1.2 0 9.60568e-005 0
1 0 0.000125725 0
0.8 0 0.000165121 0
0.6 0 0.000208685 0
0.4 0 0.000226143 0
0.2 0 0.000159004 0
1e-020 0 0 0
0.2 0 -0.000159004 0
0.4 0 -0.000226143 0
0.6 0 -0.000208685 0
0.8 0 -0.000165121 0
1 0 -0.000125725 0
1.2 0 -9.60568e-005 0
1.4 0 -7.45694e-005 0
1.6 0 -5.89503e-005 0
1.8 0 -4.74049e-005 0
2 0 -3.86987e-005 0

I want to end up with one text file that has all the data from each txt file in it being tab separated...this is the part im stuck on.

Here is what I have:

$iter=1;

$data="c://scripts//output//em1d_ip$iter.txt";

open (DATA, "$data");

foreach (<DATA>) {

($A,$B,$C,$D) = split;

$out="c:\\scripts\\modeloutput.txt";

open (OUT, ">>$out");

print OUT "$C\n";


close(OUT, "$out");

$iter++;

}


I cant get it to open each separate file and then essentially write to a "master" output file with each list being tab separated, I know that the for loop is probably not the correct command to use but it does work on an indiviual file. I assume it is possible, I dont have much programming experience and could use some assistance on the subject.


ichi
User

Jul 5, 2009, 9:32 PM

Post #2 of 7 (322 views)
Re: [Okeanos] Stuck...Need some help [In reply to] Can't Post


Code
$\ = "\n";              # set output record separator 
$, = "\t"; #set output field separator
while (<>) {
chomp;
@f = split(/\s+/, $_);
print join($,,@f);
}

usage: perl test.pl em*.txt > newfile


KevinR
Veteran


Jul 5, 2009, 9:53 PM

Post #3 of 7 (316 views)
Re: [Okeanos] Stuck...Need some help [In reply to] Can't Post

Do you have to open the em1d_ip*.txt files in numeric order or can they be opened and read and put into the new file in any order?

And do you want all the fields in the files to be put in the new file or just field "C" (as in $C) in your code:

print OUT "$C\n";
-------------------------------------------------


(This post was edited by KevinR on Jul 5, 2009, 9:55 PM)


Okeanos
New User

Jul 5, 2009, 10:11 PM

Post #4 of 7 (312 views)
Re: [KevinR] Stuck...Need some help [In reply to] Can't Post

KevinR,

No, the order in which the .txt files are opened does not really matter as long as all the data read within each individual .txt file is in the right order, and the only field I am interested in reading and writing is the "$C" field...the other numbers are not important.


(This post was edited by Okeanos on Jul 5, 2009, 10:11 PM)


KevinR
Veteran


Jul 5, 2009, 11:00 PM

Post #5 of 7 (301 views)
Re: [Okeanos] Stuck...Need some help [In reply to] Can't Post

I assumed you wanted the output of each file on one line with the data tab separated, if not you have to explain more clearly how you want the output file formatted. Untested code, so use on a few sample files first to see how it works:


Code
use strict; 
use warnings;

my @files = <c:/scripts/output/em1d_ip*.txt>;
my $out = 'c:/scripts/modeloutput.txt';
open (my $OUT, ">" , $out) or die "Can't open $out: $!";
foreach my $file (@files) {
open (my $FH, "<" , $file) or do{print "Can't open $file: $!\nSkipping to next file\n"; next;};
while(my $line = <$FH>){
print $OUT +(split(/\s+/,$line))[2],"\t";
}
print $OUT "\n";
}
close $OUT;

-------------------------------------------------


Okeanos
New User

Jul 6, 2009, 4:25 PM

Post #6 of 7 (287 views)
Re: [KevinR] Stuck...Need some help [In reply to] Can't Post

KevinR,

It definitely works and performs the operation in numerical order, many thanks for your help. Just for the sake of knowing though, lets say I wanted the data from each .txt to be put into separate columns within the output file, how would I go about doing that? would it be a different method of parsing the data?

like this...

em1d_ip1.txt
3.234
2.4234
1.4124
1.1231


instead of

3.234 1.231 .3425 .124


(This post was edited by Okeanos on Jul 6, 2009, 4:30 PM)


KevinR
Veteran


Jul 6, 2009, 4:35 PM

Post #7 of 7 (284 views)
Re: [Okeanos] Stuck...Need some help [In reply to] Can't Post

Parsing the input would be the same, its the formatting the output that will change and will be more work than doing how it is currently done. You will have to read all the file before generating any output and construct some type of data structure that can accommodate the output you want, possibly an array of arrays. It will be even more work if you wanted the output in order by ip1, ip2, ip3.
-------------------------------------------------

 
 


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

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