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

I'm having problems getting the output I need.

"founds_vuln_test" is just a list of IP addrs and an associated CVE vulnerability ID.
"CVE_data_Demo" is a csv file of CVE ID and a list of their attributes.

#!/usr/bin/perl use strict; use warnings; open (DATA, "found_vulns_test"); open (CVE_ID, "CVE_data_Demo"); my @cve_list = <CVE_ID>; while (<DATA>) { chomp; my @list = split/,/; my $vuln = $list[1]; my @matched = (grep (/$vuln/, @cve_list)); # works up to here - grep outputs a string, right? But when I get to t +he next line, I get the scalar/count output... my @matched_cve = split(',', @matched); # Then I want to go through the fields as in the snippet below: $_= $matched_cve[1]; if( /COMPLETE|[C]/i ) { $biasedConf= 100; # percentage Confidentiality Impact v +alues } elsif( /PARTIAL|[P]/i ) { $biasedConf= 50; # percentage Confidentiality Impact va +lues } elsif( /NONE|[I]/i ) { $biasedConf= 0; # percentage Confidentiality Impact + values } else { $biasedConf= 0; # percentage Confidentiality Impact + values }; }
Any help would be greatly appreciated!

Best regards,

Scott...

Replies are listed 'Best First'.
Re: Problems with grep
by citromatik (Curate) on Jan 30, 2009 at 14:11 UTC
    grep outputs a string, right? But when I get to the next line, I get the scalar/count output...
    my @matched_cve = split(',', @matched);

    grep returns a list not a scalar. Also, split evaluates the expression in scalar context that is why you are getting the count of the elements in @matched

    citromatik

Re: Problems with grep
by johngg (Canon) on Jan 30, 2009 at 15:14 UTC

    I wonder if you would be better off parsing the "CVE_data_Demo" file into a hash rather than slurping the file into an array and repeatedly greping over it. Then you would be able to do a simple hash lookup for each line of your "found_vulns_test" file. It would be useful to see a sample of the data, sanitised if necessary, to form a better judgement.

    It appears that you are matching against an alternation of, for example, 'PARTIAL' or '[P]' but you should note that square brackets are regular expression metacharacters that enclose a character class; in this case a class with a single member, 'P'. They should be escaped to match literals.

    ... } elsif( /PARTIAL|\[P\]/i ) { ...

    Again, I wonder if a hash might be better.

    ... my %biasedConfLookup = ( COMPLETE => 100, q{[C]} => 100, PARTIAL => 50, q{[P]} => 50, NONE => 0, q{[I]} => 0, ); ... $biasedConf = exists $biasedConfLookup{ uc $_ } ? $biasedConfLookup{ uc $_ } : 0; ...

    Another point; you should always check for the success or otherwise of your open statements and it is recommended that you use the three-argument form with lexical filehandles

    open my $foundVulnsFH, q{<}, q{found_vulns_test} or die qq{open: < found_vulns_test: $!\n};

    and you should avoid using the DATA filehandle for a file you open, as filehandle of that name is opened automatically for you by the perl interpreter if your script contains data inline after __DATA__ or __END__ tags.

    I hope these points are helpful.

    Cheers,

    JohnGG

Re: Problems with grep
by Anonymous Monk on Jan 30, 2009 at 14:16 UTC
    # works up to here - grep outputs a string, right? But when I get to t +he next line, I get the scalar/count output...
    Grep returns scalars, example
    D:\>perl -e"die grep /6/, 3 .. 6,6,6" 666 at -e line 1.
    your problem is you're misusing split, example
    D:\>perl -e"die split 1, 121314" 234 at -e line 1. D:\>perl -e"die split 1, 121314, 1" 121314 at -e line 1. D:\>perl -e"die split 1, 121314, 2" 21314 at -e line 1. D:\>perl -e"die split 1, 121314, 3" 2314 at -e line 1. D:\>perl -e"die split 1, 121314, 4"
    you may have intended
    my @matched_cve = map { split(',', $_) } @matched;
      Grep returns scalars, example
      D:\>perl -e"die grep /6/, 3 .. 6,6,6" 666 at -e line 1.

      grep returns a list, and in scalar context, the number of elements the expression was true.

      In your example, die outputs the list returned by grep:

      perl -e "die scalar grep /6/, 3 .. 6,6,6" 3 at -e line 1.

      What you did is essentially the same as:

      perl -e 'die qw/1 2 3/' 123 at -e line 1.

      citromatik

        | \|/ scalars<-- /|\ |
        perldoc -f grep is better :)
      I knew that I was misusing something!

      Thanks for your help...

      As a brand new member, I can only say that I am astounded by the support here. The timeliness and helpfulness of the responses is incredible! Thank you all again!

      Scott...

        Welcome. “Come, friend, and do likewise.”