On Fri, Jan 7, 2011 at 9:16 PM, S.F. <lotus.sf1...@gmail.com> wrote: > I have a data file with n columns and r row. > The first 3 columns and the first 5 rows are: > > 2 3 1 > 1 6 X > 4 0 X > X 8 X > 5 X 5 > > The "X" means missing. > How could I write a script to calculate the average by column and > replace "X" with the average? > > The output should be like: > > 2 3 1 > 1 6 3 > 4 0 3 > 3 8 3 > 5 4.25 5 > > For column 1, the counts excluding "X" is 4 with sum=12. So the > average for column 1 is 3. > For column 2, the counts excluding "X" is 4 with sum=17. So the > average for column 2 is 4.25. > For column 3, the counts excluding "X" is 2 with sum=6. So the average > for column 3 is 3.
This sounds an awful lot like a homework assignment. ;) Why don't you start by telling us in English how you might accomplish this? Then convert that to pseudo-code. It's hard to write Perl if you don't know Perl, but it's harder still to write Perl if you don't know what you're trying to write. :) It always helps when coding to figure out the problem before hand, one step at a time. -- Brandon McCaig <http://www.bamccaig.com> <bamcc...@gmail.com> V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl. Castopulence Software <http://www.castopulence.org/> <bamcc...@castopulence.org> -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/