guri guri - 19 days ago 5
Perl Question

Perl extract hash values partially and add it in database into two columns

I have this data extracted from XML file.

$v={ 'CVE-2015-5698' => {
'cve_id' => 'CVE-2015-5698',
'seq' => '2015-5698',
'CVSS_base_score' => '7.5',
'CVSS_vector' => 4
},
'CVE-2015-0812' => {
'cve_id' => 'CVE-2015-0812
'seq' => '2015-0812',
'type' => 'CVE',
'CVSS_impact_subscore' => '2.9'
}

'


Question:

1.How to extract cve_id and seq key/value pair from both CVE?

2.Which data structure should i use to save the extracted cve_id and sec so that i can easily use these values to insert in database?

Answer

Assuming that $v is a hash reference ($v={...}), and that when you say sec you mean seq, I would use an array of arrays (AoA) as the structure.

use warnings;
use strict;

my $v = { 
      'CVE-2015-5698' => {
                         'cve_id' => 'CVE-2015-5698',
                         'seq' => '2015-5698',
                         'CVSS_base_score' => '7.5',
                         'CVSS_vector' => 4
                       },
      'CVE-2015-0812' => {
                         'cve_id' => 'CVE-2015-0812',
                         'seq' => '2015-0812',
                         'type' => 'CVE',
                         'CVSS_impact_subscore' => '2.9'
                       },
};

my @cve_list;

for my $cve (keys %$v){
    push @cve_list, [$cve, $v->{$cve}{seq}];
}

for (@cve_list){
    print "cve: $_->[0], seq: $_->[1]\n";
    # do your db inserts/work here
}

Output:

cve: CVE-2015-0812, seq: 2015-0812
cve: CVE-2015-5698, seq: 2015-5698