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

Home » Public Forums » archive » Re: Removing some but not all spaces in a string
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: Removing some but not all spaces in a string [message #61758 is a reply to message #61757] Tue, 29 July 2008 04:26 Go to previous messageGo to previous message
Jeremy Bailin is currently offline  Jeremy Bailin
Messages: 618
Registered: April 2008
Senior Member
On Jul 28, 10:41 pm, Craig Markwardt
<craigm...@REMOVEcow.physics.wisc.edu> wrote:
> wlandsman <wlands...@gmail.com> writes:
>> I'm looking for suggestions for the best way remove the spaces around
>> "operators" (e.g. <. > , =) in a string but not around other
>> characters.   For example, I want the string
>
>> 'name = ngc 5548, v <15'
>
>> to become
>
>> 'name=ngc 5548, v<15'
>
> I usually do specialized string twizzling at the byte level, which is
> done in TEST2 below.  I'm not sure if TEST2 faster or more elegant
> than your TEST, but it avoids all the gluing and ungluing inherent in
> the STREGEX() method.  It's a pity there isn't a STREPLACE() which
> handles regular expressions.
>
> I also attached a version based on REPSTR() but since that sub-routine
> itself does glue/unglue, it's just hiding the mess behind a facade.
>
> Craig
>
> pro test2, st
>   space = 32b
>   ops = '<>='       ;; Operations to delete spaces
>   bops = byte(ops)
>
>   st = strtrim(strcompress(st),2)
>   if st EQ '' then return
>
>   bb = byte(st)
>   mask = 0b
>   for i = 0, n_elements(bops)-1 do begin
>     mask = mask OR [(bb EQ space   AND bb(1:*) EQ bops(i)),0b]
>     mask = mask OR [0b,(bb EQ bops(i) AND bb(1:*) EQ space)]
>   endfor
>
>   wh = where(mask EQ 0)
>   st = string(byte(bb(wh)))
>   return
> end
>
> pro test3, st
>   space = ' '
>   ops = ['<','>','=']       ;; Operations to delete spaces
>
>   st = strtrim(strcompress(st),2)
>   if st EQ '' then return
>   for i = 0, n_elements(ops)-1 do begin
>       op = ops(i)
>       st = repstr(st, ' '+op, op)
>       st = repstr(st, op+' ', op)
>   endfor
>   return
> end
>
> --
> ------------------------------------------------------------ --------------
> Craig B. Markwardt, Ph.D.      EMAIL: craigm...@REMOVEcow.physics.wisc.edu
> Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
> ------------------------------------------------------------ --------------

I know this is a cheat, but I'm a firm believer in using the right
tool for the job... and string manipulation is not IDL's.

spawn, "echo '"+string1+"' | sed -e 's/ *\([<>=]\) */\1/g'", string2

-Jeremy.
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Array has a corrupted discriptor, reliable?
Next Topic: error on stddev estimate

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

Current Time: Fri Oct 10 13:45:12 PDT 2025

Total time taken to generate the page: 0.55607 seconds