Dear Monks My apologies for a somewhat trivial question, but I'm unable to get my head around the vagaries of split. Here's my data sample:
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:
my @tmpFields = split (/\(?(\d+)\*(\w+)\)\s+/, $line);
But that gives me:
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
I then get rid of array elements that are "empty" (contain nothing but a space, or begin with spaces) using:
my @tfields = grep {!/\s+/} @tmpFields;
At this point, the arrays end up with even number of elements:
print "array length: " , scalar(@tfields), "\n", Dumper (@tfields);
and the output:
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';
However, when I convert it to a hash, I lose some elements:
my %hFields = @tfields;
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?
next if ($line !~ /^Summary/); my @tmpFields = split (/\(?(\d+)\*(\w+)\)/, $line); my @tfields = grep {!/\s+/} @tmpFields; my %hFields = @tfields;

In reply to split snafus by manav_gupta

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.