Note that the lexwhere and title parameters have spaces in them.vchtyp=I jrnltyp=D lexwhere="'in('DIS', 'DIM', 'DIR')'" title="'this i +s a title'"
There must be an easier way to parse this than what I've come up with.$hash{vchtyp} = 'I' $hash{jrnltyp = 'D'; $hash{lexwhere} = "'in('DIS', 'DIM', 'DIR')'"; $hash{title} = "'this is a test'";
Does anyone have any suggestions?sub parseArgs { my (@args) = @_; my @chars = split(// , join(' ', @args)); my %params = (); my $label = ''; my $value = ''; my $inLabel = 0; my $inValue = 0; my $doubleQuotes = 0; my $singleQuotes = 0; my $isSpace = 0; my $isEquals = 0; my $isDoubleQuote = 0; my $isSingleQuote = 0; for my $char (@chars) { $isSpace = $char =~ /\s/; $isEquals = $char eq '='; $isDoubleQuote = $char eq '"'; $isSingleQuote = $char eq "'"; if ($inLabel) { if ($isEquals) { $inLabel = 0; $inValue = 1; } else { $label .= $char; } } elsif ($inValue) { $doubleQuotes++ if $isDoubleQuote; $singleQuotes++ if $isSingleQuote; if ($isSpace) { if (($singleQuotes % 2) == 0 && ($doubleQuotes % 2) == 0) +{ $inValue = 0; $params{$label} = $value; $label = ''; $value = ''; } else { $value .= $char; } } else { $value .= $char; } } elsif (!$isSpace) { $inLabel = 1; $label .= $char; } } if ($inValue && $label && $value) { $params{$label} = $value; } return %params; }
Edit by thelenm: changed pre tags to code tags
In reply to parsing arguments by mifflin
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |