I made it like this:
#The idea is to read the first line of the file and see how many colum +ns we have, then we proceed accordingly, column-by-column open (INFILE10, '<', 'ex1.dat') or die "File ex1.dat does not exist!\n +"; my $firstLine = <INFILE10>; close INFILE10; my @array_firstLine=split(/\t/, $firstLine); my $total_columns=scalar(@array_firstLine); print "This file has $total_columns columns in total.\n"; for(my $k=1; $k<=$total_columns; $k++) { print "Calculate sum for column $k\n"; my $wanted_column_number=$k; #this is the column that we want to +sum up each time, until we finish the columns my $sum_of_column=0; open (INFILE10, '<', 'ex1.dat') or die "File ex1.dat does not exist! +\n"; while( my $line10 = <INFILE10>) { my @split_line10 = split(/\t/, $line10); my $respective_element = $split_line10[$k-1]; $sum_of_column = $sum_of_column + $respective_element; } close INFILE10; print "The sum for column $k is: $sum_of_column.\n"; }

In reply to Re^2: How can you make this script general? by Anonymous Monk
in thread How can you make this script general? by Anonymous Monk

Title:
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, details, 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, summary, 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.