in reply to Re: Finding the sum of individual columns
in thread Finding the sum of individual columns

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

Replies are listed 'Best First'.
Re^3: Finding the sum of individual columns
by Corion (Patriarch) on Apr 13, 2010 at 09:13 UTC

    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!!!