Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Looping through text and adding values to hash

by smford22 (Initiate)
on Jul 10, 2013 at 17:10 UTC ( #1043505=perlquestion: print w/ replies, xml ) Need Help??
smford22 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I have written a script that wraps Java Keytool in perl to list the certificates in a keystore. I am using a foreach loop through the output from Keytool and pull the name of the certificate and the expiration date. I want to put the values into an array of hashes but I cannot quite understand how to get that to happen. What I am trying to get to is something like this...
@AoH = ( { $certName => $validTo, $certName => $validTo, } );
I declare a global array of hashes at the beginning of the script with:
our @foundCertificates();
I have the following subroutine setup to parse through the output from the keytool to pull the certificate names and the date they are valid to:
sub parseResponse { my $out = shift; my $certname; my $validTo; my $retMonth; foreach my $line (split ("\n", $out)) { if ($line =~ m/^(Alias\sname:\s)(.+)/) { $certname = $2; #print "$certname\n"; } elsif ($line =~ m/^(Valid\sfrom:\s\w+\s)(\w+)\s(\d\d)\s(\d\d:\ +d\d:\d\d)\s\w+\s(\d+)\s(until:)\s\w+\s(\w+)\s(\d\d)\s(\d\d):(\d\d):(\ +d\d)\s\w+\s(\d\d\d\d)/) { $retMonth = &convertMonth($7); $validTo = timegm($11,$10,$9,$8,$retMonth,$12); #print "$validTo\n"; } } #print "$certname - $validTo\n"; }
I know that my regular expressions work as the script captures the three certificates in my keystore, but what I cannot figure out to do is how to push the $certname and the $validTo in the array as a pair. I am not sure what other information I can provide, but any help is greatly appreciated. Thanks, Scott

Comment on Looping through text and adding values to hash
Select or Download Code
Re: Looping through text and adding values to hash
by ww (Bishop) on Jul 10, 2013 at 18:21 UTC

    If you don't have some un-stated reason for using a hash (narrative, paras 1 and 2), and can take an array (narrative, para3), you need merely concatenate the $certName and  $validTo vars into a single, third var that you then push to your array -- as a pair, perhaps separated by a space. Here are three slightly variant approaches:

    #!/usr/bin/perl use 5.016; my $certName = 'Hans'; my $validTo = '20130710'; my $thirdvar .= $certName; $thirdvar .= ' '; $thirdvar .= $validTo; say "\$thirdvar: $thirdvar \n"; my $fourthvar = "$certName, $validTo"; say "\$fourthvar: $fourthvar \n"; my $fifthvar = $certName . " " . $validTo; say "\$fifthvar: $fifthvar"; =head $thirdvar: Hans 20130710 $fourthvar: Hans, 20130710 $fifthvar: Hans 20130710 =cut
    If I've misconstrued your question or the logic needed to answer it, I offer my apologies to all those electrons which were inconvenienced by the creation of this post.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1043505]
Approved by Old_Gray_Bear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (12)
As of 2014-10-21 09:10 GMT
Find Nodes?
    Voting Booth?

    For retirement, I am banking on:

    Results (99 votes), past polls