Oh wise ones... I have the need to parse a file that lists variables. The file format is something like *rvd*daemonPort : 7500 or hostname*rvd*daemonPort : 7500 or could even extend to hostname*rvd*RTD*daemonPort : 7500. So the variables are scoped either globally or locally. Also, the variables will be unknown to me until I parse the file.
So, Parsing the file is not difficult. The issue for me is putting the data into a usable array or hash.
while( <FILE> ) {
@tmp = split(/\*/);
SWITCH: {
for ($i = 0; $i <= $#tmp; $i++) {
if ($tmp[$i]) {
# make sure the first value is the key.
print "Setting key : $KEY\n", if ($Verbose);
$KEY = "$tmp[$i]";
# populate the key with the other values
# hostname*rvd*daemonPort : 7500
for ($i; $i <= $#tmp; $i++) {
# reached the last var populate the va
+lue
if ( $tmp[$i] =~ /^(\w+)\s+\:\s+([A-Z.
+0-9_\/])/ig) {
$Vars{$KEY}{$tmp[$i - 1]}{$1} => $2;
print "$tmp[$i]";
}else{
$Vars{$KEY}{$tmp[$i]};
}
}
last SWITCH;
}
}
}
} # while <file>
Obviously this is not going to work correctly. The real problem is getting a hash that looks like this
$hash{rvd}{RTD}{daemonPort} => 7500;
without knowing how deep I am going to go.
Any ideas out there?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.