Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: How to add the rows by its similar row names using perl?

by kcott (Archbishop)
on May 05, 2017 at 07:42 UTC ( [id://1189554]=note: print w/replies, xml ) Need Help??


in reply to How to add the rows by its similar row names using perl?

G'day gopikavi,

Welcome to the Monastery.

I see ++huck has pointed out your indexing error and usage of a hash. Here's an alternative method for capturing the data:

#!/usr/bin/env perl -l use strict; use warnings; my %slot; my $re = qr{([a-z]{3}).*?(\d+)/(\d+)}; while (<DATA>) { if (/$re/) { $slot{$1}{used} += $2; $slot{$1}{free} += $3; } } print "$_: @{$slot{$_}}{qw{used free}}" for sort keys %slot; __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}

Output:

asf: 2 24 cad: 0 12 cas: 0 24 dsf: 0 16

— Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1189554]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-24 18:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found