manav_gupta has asked for the wisdom of the Perl Monks concerning the following question:
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_Memory_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)I need to produce a total for each of the items (such as Cisco_Power_Supply, etc). So I decided to use split to extract each item, and its count:
But that gives me:my @tmpFields = split (/\(?(\d+)\*(\w+)\)\s+/, $line);
I then get rid of array elements that are "empty" (contain nothing but a space, or begin with spaces) using:i = 0 , Summary 51.58.214.48/dw109998bsw45 -> i = 1 , 1 i = 2 , Cisco_Power_Supply i = 3 , i = 4 , 1 i = 5 , Cisco_CPU_Unit i = 6 , i = 7 , 7 i = 8 , IETF_IF i = 9 , i = 10 , 1 i = 11 , Cisco_Fan_Unit i = 12 , i = 13 , 1 i = 14 , 1213_Device i = 15 , i = 16 , 2 i = 17 , Cisco_Memory_Pool
At this point, the arrays end up with even number of elements:my @tfields = grep {!/\s+/} @tmpFields;
and the output:print "array length: " , scalar(@tfields), "\n", Dumper (@tfields);
However, when I convert it to a hash, I lose some elements:array length: 12 $VAR1 = '1'; $VAR2 = 'Cisco_Power_Supply'; $VAR3 = '1'; $VAR4 = 'Cisco_CPU_Unit'; $VAR5 = '7'; $VAR6 = 'IETF_IF'; $VAR7 = '1'; $VAR8 = 'Cisco_Fan_Unit'; $VAR9 = '1'; $VAR10 = '1213_Device'; $VAR11 = '2'; $VAR12 = 'Cisco_Memory_Pool';
So how do I convert it to a hash and not lose any array elements? Of course, I'm sure this whole code snippet can be improved as well - so please could you help?my %hFields = @tfields;
next if ($line !~ /^Summary/); my @tmpFields = split (/\(?(\d+)\*(\w+)\)/, $line); my @tfields = grep {!/\s+/} @tmpFields; my %hFields = @tfields;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: split snafus
by GrandFather (Saint) on Jun 17, 2008 at 11:58 UTC | |
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 | |
|
Re: split snafus
by moritz (Cardinal) on Jun 17, 2008 at 11:34 UTC | |
by manav_gupta (Acolyte) on Jun 17, 2008 at 11:40 UTC |