[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] How to parse quoted text?



>Hy everyone.
>
>I just started tackling an interesting problem: I have a file that has
>information seperated by spaces. Each line is a record. Each item is
>enclosed in quotes. For example:
>
>add telecom "856473" "Louis Chretien" "" "Administrator"
>
>you get the idea... ;-)
>
>What would be the best way to parse each item into a separate variable in
>Perl? I first tought of split, but split won't account for the quotes
>enclosing space (split would seperate between "Louis" and "Chretien")
>
>I thought of using a regular expression, but no pattern is obvious.
>
>Is there a "canonical" way to do this?
>
>any help would be appreciated.
>
>Louis Chretien
>lchretien@jdeq.com

I'm not sure what the canonical way would be: there seems to be a genuine problem in using split, 
which is that the field separator (" "  ie quote space quote) and the fields (".*") overlap. 

Here is a beginner's suggestion to accomplish your spec:


@fields = split(/" "/); # assuming the record has already been read into $_
                                        # you'll want to use split(/" +"/) if more than one space is possible between quote marks

foreach $field (@fields)        {
        if ( $field !~ m/"$/ )  $field = $field."\"";   # adding the final quote, which overlapped with the separator
        if ( $field !~ m/^"/ )  $field = "\"".$field;   # adding the initial quote, which overlapped with the separator
}
# That's it

**I haven't tested this out! I'm assuming that you will do that on your data sets :-)

Good luck,
AK