I have this script that reads in lines from a configuration file, processes
them, and then stores them in a hash. Everything is working (mostly) but I
really don't like the snippet below and wanted to see if anyone could
suggest a better solution. I would call better a syntactically shorter,
less processor intensive, or just plain faster. Here's the trick to the
below sample though. I know that I will get a line that looks like...
NAME DELIM VALUE
The white spaces between the values are required but I have no idea what
NAME, DELIM, or VALUE will be. I know they will not contain new lines but
they may be quoted strings containing spaces themselves as in the code
below. I'm currently requiring that they cannot contain quotes but it would
be good if I could remove that restriction.
So right now I split the line on white space and then put things together
again in a 'for' loop. Does anyone have any suggestions?
Thanks,
Peter C.
use strict;
use warnings;
local $_ = 'name = "quoted string with space"';
my @pair = split;
#repair damage done to a quoted string
for (my $i = 0; $i < $#pair; $i++) {
if ($pair[$i] =~ /^['"]/) {
until (!defined($pair[$i])
|| !defined($pair[$i+1])
|| $pair[$i] =~ /['"]$/) {
$pair[$i] .= " " . $pair[$i+1];
splice @pair, $i+1, 1;
}
$i++;
}
}
print "$_\n" for (@pair)