in reply to Finding the sum of individual columns

So: what did you try? Where do you have problems?

You probably want to

  1. read the file line-by-line (see open )
  2. split the line on blanks (\s)
  3. create a sum for each element of the split-result (except the first)

Pretty straightforward, isn't it :-)

HTH, Rata

Replies are listed 'Best First'.
Re^2: Finding the sum of individual columns
by thillai_selvan (Initiate) on Apr 13, 2010 at 09:11 UTC
    I have tried like this.
    open FH,"<data_input" or die "Can't open file : $!\n"; my @line; while ( <FH> ) { if ( /(obj[1-9]*)/ ) { my $line = $'; #for getting other than obj column in the line my $val=0; $val += split(' ',$line); print $val; } }
    But I can't get the sum in the $val variable. Can you please help me

      split returns a list. Adding a list to a scalar does not really make sense in this context. Maybe you want a loop?

      my @items = qw(1 2 3 4 5 6); my $val = 10; $val += $_ for @items;
        split returns a list.

        In list context but in scalar context it returns the number of fields in the list and puts the list into @_ and issues a warning.

        Yeah. It works great. Thanks!!!