green_lakers has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Counting matching Lines of an Array
by kennethk (Abbot) on Jan 20, 2009 at 16:06 UTC

    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.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Counting matching Lines of an Array
by jeffa (Bishop) on Jan 20, 2009 at 16:02 UTC

    If this were me tasked with the job, i would spend my time writing a parser to parse the data and insert the results into a proper Database. If i did not know how to do those things, i would seek out training. I actually went to college to learn how to do this stuff.

    UPDATE to kennethk: see what happens when you give a man a fish instead of teaching him how to catch his own? He just asks you for more fish.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)