Hi,
I'm not sure that I got it right, but let's try:
considering you want to parse several substrings from the line, each of them
can be either bareword (== sequence of non-whitespace characters) or a quoted
strings:
my $line = 'whatever "this" \'line is\'';
$line =~ s/\s*$//;
my @parts;
while ($line ne '') {
if ($line =~ m/^\s*(['"])((?:(?:\\.)|[^\\])*?)\1(.*)/) {
push @parts, $2;
$line = $3;
} elsif ($line =~ m/^\s*(\S+)(.*)/) {
push @parts, $1;
$line = $2;
}
}
foreach (@parts) { print "$_\n"; }
On Wednesday 06 June 2001 02:49, Peter Cornelius wrote:
> 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)
--
Ondrej Par
Internet Securities
Software Engineer
e-mail: [EMAIL PROTECTED]
Phone: +420 2 222 543 45 ext. 112