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
resultuse 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}
So tell the truth now, was this course homework?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
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |