thillai_selvan has asked for the wisdom of the Perl Monks concerning the following question:

I am having input file like this.
obj1 0.01953 0.34576 0.04418 0.01249 obj2 0.78140 0.00015 0.02175 0.19468 0.03274 obj3 0.00014 0.00016 0.00014 1.03222 0.24256 0.00014 0.00014 0.00014 0 +.00016 etc
I want to find the sum in the individual lines what ever the data is there other than the obj. So how can I achieve this?

Replies are listed 'Best First'.
Re: Finding the sum of individual columns
by Ratazong (Monsignor) on Apr 13, 2010 at 09:01 UTC

    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
      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;
Re: Finding the sum of individual columns
by umasuresh (Hermit) on Apr 13, 2010 at 14:26 UTC