comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: splitting strings
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: splitting strings [message #42425] Sat, 05 February 2005 07:21 Go to previous message
KM is currently offline  KM
Messages: 29
Registered: October 2004
Junior Member
On Fri, 4 Feb 2005, Benjamin Hornberger wrote:
> Benjamin Hornberger wrote:
>> Hi all,
>>
>> I would like to split a string by whitespace characters, while
>> anything between quotes should be recognized as one elements
>> (even if it contains whitespace). Let's say I have the string
>>
>> 'cat dog "ground hog" bird'
>>
>> I want to split it into ['cat', 'dog', 'ground hog', 'bird'].
>
> Ok, here we go. Found on http://php.net/split and translated to
> IDL with some modifications. Any comments are welcome.

Looks like it works. It is loopy, but I doubt you need much speed,
unless you have a lot of strings. That being said, I got this
working before you posted that with your 2nd example. The 2nd
example breaks the code, but works with some minor modifications.
The following is not very robust.

str = 'cat dog "ground hog" "bird"'
str2 = strsplit(str,/extract) ; split on whitespace
split = STREGEX(str2,'"') ; find all "'s
split_loc = where( split gt 0, n )-1 ; use only the 2st half
str3 = str2
str3[ split_loc ] = str3[split_loc]+" "+str3[split_loc+1] ; patch
str3[ split_loc+1 ] = "rem" ; remove the 2nd half of the bad split
str3 = str3[ where( str3 NE 'rem' ) ]


Also, unrelated to the above:
IDL> print, stregex(str,'".*( ).*',/subexpr)
prints both the location of the " and the space.

So I'm pretty sure there is a way to do this without loops...

-k.
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Array extraction, multiple slices from paired values
Next Topic: Drawing lines on image and obtaining points of intersection

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Thu Oct 09 07:23:00 PDT 2025

Total time taken to generate the page: 1.52042 seconds