in reply to split snafus
I'd use a regex to pull out the fields you are interested in then use split to chop em up. With a little cunning you can leverage map and a for (as a statement modifier) to get everything done in a fairly compact, but lucid way. Consider:
use warnings; use strict; my $data = <<'DATA'; Summary 51.58.214.48/dw109998bsw45 -> (1*Cisco_Power_Supply) (1*Cisco_ +CPU_Unit) (7*IETF_IF) (1*Cisco_Fan_Unit) (1*1213_Device) (2*Cisco_Mem +ory_Pool) Summary 51.58.220.21/dw108432bsw25 -> (6*Cisco_Power_Supply +) (1*Cisco_CPU_Unit) (333*IETF_IF) (6*Cisco_Fan_Unit) (1*1213_Device) + (2*Cisco_Memory_Pool)DATA DATA my %totals; $totals{$_->[1]} += $_->[0] for map {[split '\*']} $data =~ /\(([^)]*) +\)/g; print "$_: $totals{$_}\n" for sort keys %totals;
Prints:
1213_Device: 2 Cisco_CPU_Unit: 2 Cisco_Fan_Unit: 7 Cisco_Memory_Pool: 4 Cisco_Power_Supply: 7 IETF_IF: 340
Note the trick of putting the split inside a [] pair to create a two element array for each item the regex generates. The chunk to the left of the for builds a hash of totals keyed by item type.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: split snafus
by manav_gupta (Acolyte) on Jun 17, 2008 at 12:17 UTC | |
by jwkrahn (Abbot) on Jun 17, 2008 at 12:25 UTC | |
by manav_gupta (Acolyte) on Jun 05, 2009 at 14:49 UTC | |
by planetscape (Chancellor) on Jun 18, 2008 at 16:12 UTC | |
by manav_gupta (Acolyte) on Jun 05, 2009 at 14:50 UTC |