Gregory Machin wrote:
Hi
I'm writing a script to pass a config file and put the values in a hash,
with the key being the directive and the value bing the options,
but some directives don't have options, so in that case i want to store the
directive as the value so that for completeness..

i'm using the follwing regex /(.+)[\s+\n](.+)/    where $1 is the directive

What is the purpose of \n?

and $2 is the option for the directive

user nobody
group nobody
persist-key
persist-tun

this is the logic that is suposed to do the work.
if ($_ =~ /^persist-key/){ /(.+)[\s+\n](.+)/ ; if ( $2 == '' ) { $_ = $1} ;
> $directive{persist_key} = $_; }

You should always test if the match operator succeeded before you use $1 and $2, or you'll get incorrect results sometimes. Also, the '==' operator is for numbers; use 'eq' for strings.

if ($_ =~ /^persist-key/) {
    if (/(.+)[\s+\n](.+)/) {
        if ( $2 eq '' ) { $_ = $1 }
    }
    $directive{persist_key} = $_;
}


but I still end up with an empty field ...

Any ideas..



Yes, try this:

if (m/^([\w-]+)(?: +(.*))?$/) {
    $directive{$1} = $2 || $1;
}




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to