Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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 }

In reply to Appending new column data to CSV file? by rnaeye

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (5)
    As of 2018-04-26 04:49 GMT
    Find Nodes?
      Voting Booth?