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?
In reply to Regex random values pattern match and code efficiency by Mark.Allan
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |