rnaeye has asked for the wisdom of the Perl Monks concerning the following question:
Hi guys, I have following code that takes multiple files, split fields, do simple arithmetic operation, and write results of each file into a separate output file. Now, I need to modify the code, and print the result of each input file as a seperate column in single output file (instead of multiple output files). I was wondering if you guys can give me a pointer. Thank you.
#!/usr/bin/perl use warnings; use 5.12.4; # USAGE: <perlscript> <*.txt> my (%hash, $key, $value); my ($col1, $col5, $sum, $rowdata, $line, $outfile, $infile, @column5); foreach my $file (@ARGV) { #name output file $outfile = $file . '.csv'; #file check die "Oops! A file called '$outfile' already exists.\n" if -e $outf +ile; open (IN, "<", "$file" ) or die "Cannot open file $!"; open (OUT, ">", "$outfile") or die "Cannot open file: $outfile + $!"; while (<IN>) { chomp ($line = $_); next if $line =~ /NoCoordinateCount.*/; next if $line =~ /^$/; ($col1, undef, undef, undef, $col5 ) = split(/\s/, $line); $sum += $col5; $key = $col1; $value = $col5; $hash{$key} = $value; } foreach (sort keys %hash) { print OUT $_, "," , $hash{$_}/$sum, "\n"; } $sum = 0; #reset sum for each file }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Appending new column data to CSV file?
by moritz (Cardinal) on Aug 04, 2013 at 16:44 UTC | |
|
Re: Appending new column data to CSV file?
by Cristoforo (Curate) on Aug 04, 2013 at 20:47 UTC | |
|
Re: Appending new column data to CSV file?
by Laurent_R (Canon) on Aug 04, 2013 at 19:06 UTC | |
|
Re: Appending new column data to CSV file?
by locked_user sundialsvc4 (Abbot) on Aug 05, 2013 at 12:35 UTC |