Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Regex random values pattern match and code efficiency

by Mark.Allan (Sexton)
on Sep 11, 2013 at 20:17 UTC ( #1053560=perlquestion: print w/ replies, xml ) Need Help??
Mark.Allan has asked for the wisdom of the Perl Monks concerning the following question:

I have two types of question for the monks out there today. One is a "How do I?" and the other is a "Where can I"!

I have two files which I am parsing, this first files structure is as follows:

DEPLOY_TIME=300 DEPLOY_TYPE=OS DEPLOY_PARAMS=$DOMAIN{HOST},$DOMAIN{IP},$DOMAIN{CONFIG} DEPLOY_DEFAULT_STARTUP=$DOMAIN{STARTUP.INI} DEPLOY_DEBUG=Y DEFAULT_EXPIRY=900

I need to pattern match for the values, not too difficult for the standard key=value pairs, but the "how can I" pattern match for the DEPLOY+PARAMS. again not that difficult if the value of DEPLOY_PARAMS was the same all the time but another occurence of this key in another file might look like this

DEPLOY_PARAMS=$ENV{HOST}

Without the config and IP values and so on. I read maybe regex assertion may be able to help me?

A snippet of code so far is as follows

my ($dtime,$dtype); open (FH, "<$file") || die ("can't open file $file"); while (<FH>) { next if (/^#/); next unless /\S/; while (m<\DEPLOY_TIME=(\d+)>gx) { $dtime = $1;} while (m<DEPLOY_TYPE=(\S+)>gx) { $dtype = $1;} # NEXT PATTERN NOT SURE ABOUT!! }

The second file as a structure as follows. I need to extract all values between the ' ' (6 in all)

keyAttributes('SURNAME', 'GIVENNAME', 'TITLE', 'SEX', 'ADDRESS', 'COUNTRY').

eg/ keyAttributes('DOE', 'John', 'Mr', 'Male', 'New York', 'US').

Now this again not too difficult to match with the following code

if (/keyAttributes\('(.*?)',\s'(.*?)',\s'(.*?)',\s'(.*?)',\s'(.*?)',\ +s'(.*?).+/) { $surname = $1; $given = $2; $title = $3; $sex = $4; $address = $5; $country = $6; }

but this leads me to my 2nd type of question, "where can I" improve BOTH chunks of code to make the perl more efficient and elegant, im always looking for tips on improvement from the gurus, please bear in mind im restricted to an old 5.8 perl, maybe I can use an array for regex for example?

Comment on Regex random values pattern match and code efficiency
Select or Download Code
Re: Regex random values pattern match and code efficiency
by moritz (Cardinal) on Sep 11, 2013 at 20:32 UTC

    As for the first question, I'd do something like this:

    use strict; use warnings; my %h; while (<DATA>) { chomp; my ($key, $value) = split /=/, $_, 2; $h{$key} = $value; } print $h{DEPLOY_PARAMS}, "\n"; __DATA__ DEPLOY_TIME=300 DEPLOY_TYPE=OS DEPLOY_PARAMS=$DOMAIN{HOST},$DOMAIN{IP},$DOMAIN{CONFIG} DEPLOY_DEFAULT_STARTUP=$DOMAIN{STARTUP.INI} DEPLOY_DEBUG=Y DEFAULT_EXPIRY=900

    (That is, parse the whole file with the same code, instead of parsing each value with a different regex).

      Thanks for that advice, just an update to the first question which critically I missed off, I need only the values between {} for DEPLOY_PARAMS and not the entire string

        just a basic regex loop.. try to adapt this..
        use strict; my $value='$DOMAIN{HOST},$DOMAIN{IP},$DOMAIN{CONFIG}'; my @data = split(',',$value); foreach my $attributes (@data) { $attributes=~ m/(DOMAIN){(.+?)}/; print $2."\n"; }
        prints:
        HOST
        IP
        CONFIG

        kindly perlig

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2014-10-26 05:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (151 votes), past polls