in reply to Splitting on double quotes

First things first: the line split (/"/,$line) by itself doesn't do much. split returns a list, so you should do something like

my @elements=split (/"/,$line)

That gives you 'type=','car','make=' etc...

Not what you wanted? Thought so. In order to also strip the "="-signs, you'll want to add them to the regex in the split:

my @elements=split (/=?"/,$line)

Added bonus: If you then do my %hash = (@elements);, you get a nice little hash:

%hash = { type => 'car', ofmake => 'Ford', color => 'red'};

Hope that helps,
BrotherAde

Replies are listed 'Best First'.
Re: Re: Splitting on double quotes
by mirod (Canon) on Aug 12, 2002 at 09:06 UTC

    The hash assignment is clever, and it works in this case, but if you run under -w (and I hope you do!) and the line you are processing happens not to end with a " then there will be an odd number of elements in the elements array (the last element of the array will be the text after the last attribute), and you will get a warning Odd number of elements in hash assignment at....

    --
    mirod
    The Error Message is GOD - MJD