You should start every script with use strict; use warnings; - it will save you a lot of headaches.

The appropriate way to access an array element is $array[0], not @array[0] as you have written. Note that the first element of an array is index 0, not 1. Using the @ sigil is only appropriate when you referencing multiple points (such as with an entire array or array slice). perl understands what you wrote, but it's considered poor form. Also poor form, though not considered as harshly, is initializing with an empty string ("" or '') - you should use undef instead for readibility reasons.

In place of nested if-else clauses, use if-elsif-else clauses - large switch blocks are much more legible this way and you don't end up with massive indents. It will also help you avoid empty cases, as you have here.

Since you are attempting to count records w/ a primary key (I'm not sure if you are using 958439 or MS08-074 as your intended primary), the choice here is a hash, not an array. A larger data set would be helpful in regards to understanding intent. In any case, as best as I can tell, this does what you meant to do.

use strict; use warnings; my @sorted; $sorted[0] = 'ABCDEF 958439 MS08-074 Security Update for the 2007 Micr +osoft Office System (KB958439) 2009-01-08'; $sorted[1] = 'FFFFFF 958439 MS08-074 Security Update for the 2007 Micr +osoft Office System (KB958439) 2009-01-09'; my $current = undef; my $prev = undef; my %filtered = (); foreach my $line (@sorted){ my @split = split(/\s/, $line); if (defined $filtered{$split[1]}) { substr($filtered{$split[1]},0,1) = (++substr($filtered{$split[ +1]},0,1)); $filtered{$split[1]} .= "\t$split[$#split]"; } else { $filtered{$split[1]} = "1\t$split[1]\t$split[0]\t$split[2]\t$s +plit[$#split]"; } } print "\nTOTAL\tBulletinID\tKBID\t\tdate\n"; foreach my $line (values %filtered){ print "$line\n"; } print "\n";

Update: I just noted that in the threads 722340, 728096, 734265, 735819, 737327 and 737401 you ask variants on the same problem and have received a good deal of assistance on this topic. It's fairly likely that after working on the same problem for nearly 3 months you are tired of fighting with it. If you are still having difficulties at the level you appear to be (based on your posted code), I would strongly suggest finding a good text book (e.g. Programming Perl or Learning Perl from O'Reilly) or take an intro class. Alternatively, it may be cheaper if you are in an industrial setting to outsource this project - paying for 1 day of a good programmer's time is likely cheaper than 3 months of your wages.


In reply to Re: Counting matching Lines of an Array by kennethk
in thread Counting matching Lines of an Array by green_lakers

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.