Lets see.

perl uses what is called a zero-origin, columns are numbered starting at zero, ie 0,1,2,3 so what you considered the 3rd column via $slots_used += (split(/\s+/,$_))[3]; is actualy the 4th column.

when you split via  split(/\s+/,$_) you only split on whitespaces, but it seems you also need to split column 3 by the slash to get what you want.

Just out of cleanness you probably should have chomped your input to get rid of the newline

And it probably makes sense to get rid of the squiggly braces too

use strict; use warnings; my %namesum; while (my $line=<DATA>) { chomp $line; next unless $line; $line=~s/[\{\}]//g; my @parts=split(/\s+/,$line); my ($slots_used,$slots_free)=split('/',$parts[2]); my $name=substr($parts[0],0,3); $namesum{$name}[0]+=$slots_used; $namesum{$name}[1]+=$slots_free; } for my $key (sort keys %namesum){ print "\nname:".$key."\n"; print "total slots used:".$namesum{$key}[0]."\n"; print "total slots free:".$namesum{$key}[1]."\n"; } __DATA__ {asf192lin1 C 0/8 0 0 0 4503 16 2922 44316 1} {asf256lin10 + 2/16 15 0 0 4641 16 2926 194108 0} {cad192lin1 C 0/12 2 0 0 1432 12 3397 179605 0} {cas256lin1 C 0/12 50 0 56 3992 12 3397 165099 0} {cas192lin11 C 0/12 50 0 56 3992 12 3397 165099 0} {dsf192lin6 + 0/16 0 0 0 4751 16 2930 179123 0}
result
name:asf total slots used:2 total slots free:24 name:cad total slots used:0 total slots free:12 name:cas total slots used:0 total slots free:24 name:dsf total slots used:0 total slots free:16
So tell the truth now, was this course homework?


In reply to Re: How to add the rows by its similar row names using perl? by huck
in thread How to add the rows by its similar row names using perl? by gopikavi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.