http://qs321.pair.com?node_id=950318


in reply to average a column in tab-delimited file

Well, the general algorithm might look like this:

First, open your file using open().

Then, in cycle, you read your file line by line, split it using split() or s///, find values of your interest, do calculations and form a content for new file in an array of lines. After cycle is done, just close this file, maybe rename it for backup version, then open it for write and push there your array with newly formed lines.
  • Comment on Re: average a column in tab-delimited file

Replies are listed 'Best First'.
Re^2: average a column in tab-delimited file
by naturalsciences (Beadle) on Jan 27, 2012 at 17:20 UTC

    Yep,

    you just can't start reading columns in a file. Cause there ain't any. There ain't no lines in a file actually either.

    There is no spoooon.

    Well that is - a text file in your computer should be just a big string of values. Those lines you see in your text editor are there only because once in a while in this big string there are line break characters, so your text editor can know when to make a break. No program can just bam go and start reading "columns" from this.

    So you have to make those columns yourself. As fisher said - the basic algorithm is to take your file line by line, split those lines(by whatever separates your values(whitespace,comma or tab), then you take each fourth(counting from zero) element from those split lines(you were looking for an average of third column if i remember correctly. So if you append those fourth elements in an array, print out somewhere or whatnot, then you've got your third column.

      To make life easier for you I will post a piece of code in here. Earlier today I had to transpose a large text file so I blurted out this piece of code. (Takes the rows from a file and makes them into a column)

      It is a rather stupid code. (I'm a noob). For example it would not be prudent to actually load all the file into an array, but better to do it line by line in a while($line=<>) loop. etc. But Being a rather stupid code it should also be quite readily understandable and demonstrates the idea of getting columns out from your lines pretty well.

      #!/usr/bin/perl -w use strict; use warnings; my @data=<>; my @column=(); for (my $count=3;$count<=4215;$count++) { foreach (@data) { my @row=split(/\t/, $_); push (@column,$row[$count]); } print "@column\n"; @column=() }

      As you can see this piece of code was used to extract columns 3 to 4215. You only need to extract one single column right now. Enjoy!