azzazzello
Technical User
Hi,
I have a file with a million lines in the following format.
It is basically a list of key/values pairs separated by a space. Where a value is NOT encased in quotes, it will have only alphanumerics or a dot or a minus sign. Where it IS encased in double quotes, it is freeform and can have any characters (including space and = sign) except for a double quote. I need to convert these into a hash of key/values
I have the following to parse them, but something tells me there is a faster way of doing it. Any ideas?
I have a file with a million lines in the following format.
Code:
field1=val1 field2="val2" field3=""
It is basically a list of key/values pairs separated by a space. Where a value is NOT encased in quotes, it will have only alphanumerics or a dot or a minus sign. Where it IS encased in double quotes, it is freeform and can have any characters (including space and = sign) except for a double quote. I need to convert these into a hash of key/values
I have the following to parse them, but something tells me there is a faster way of doing it. Any ideas?
Code:
my %vals;
while ($line =~ /([^=\"]+)=((\"(.*?)\")|([\-\w\.]*?))\s+/g)
{
($key,$val) = ($1,$2);
$val =~ s/"//g if $val;
$vals{$key} = defined($val) ? $val : "";
}