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 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: Removing some but not all spaces in a string [message #61746] Tue, 29 July 2008 09:09
humanumbrella is currently offline  humanumbrella
Messages: 52
Registered: June 2008
Member
On Jul 29, 11:40 am, wlandsman <wlands...@gmail.com> wrote:
> That's OK for that specific string, but I'd like to make it work for
> an arbitrary string.

I realize (: -- I figured all of your data was formatted in the same
way... (:
ie: str = NGC #, v<#

have fun~~
Cheers,
--Justin
Re: Removing some but not all spaces in a string [message #61749 is a reply to message #61746] Tue, 29 July 2008 08:43 Go to previous message
Craig Markwardt is currently offline  Craig Markwardt
Messages: 1869
Registered: November 1996
Senior Member
David Fanning <news@dfanning.com> writes:
> Craig Markwardt writes:
>
>> It's a pity there isn't a STREPLACE() which
>> handles regular expressions.
>
> If I am not mistaken, Mike Galloy has a version
> called STR_REPLACE that handles regular expressions.
> I found it with his white paper on regular expressions
> (a must read, by the way). Don't have a link, but
> certainly it can be found on Mike's web page:
>
> http://www.michaelgalloy.com/

It looks nice. But what I should have said was, it's a pity there
isn't a *NATIVE* version of STREPLACE() which handles regular
expressions. All of the string replacement functions do lots of
slicing and dicing of the string, which Wayne is already quite capable
of :-)

Craig

--
------------------------------------------------------------ --------------
Craig B. Markwardt, Ph.D. EMAIL: craigmnet@REMOVEcow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
------------------------------------------------------------ --------------
Re: Removing some but not all spaces in a string [message #61750 is a reply to message #61749] Tue, 29 July 2008 08:40 Go to previous message
wlandsman is currently offline  wlandsman
Messages: 743
Registered: June 2000
Senior Member
On Jul 29, 11:27 am, David Fanning <n...@dfanning.com> wrote:
> If I am not mistaken, Mike Galloy has a version
> called STR_REPLACE that handles regular expressions.
> I found it with his white paper on regular expressions
> (a must read, by the way). Don't have a link, but
> certainly it can be found on Mike's web page:
>
STR_REPLACE is available in the ITTVIS contrib library
http://www.ittvis.com/codebank/search.asp?FID=311
Since it accepts regular expressions, I suspect I can use it instead
of Jeremy's sed command. But first I have to parse how that sed
command works.

repstr.pro is from the Astron library
http://idlastro.gsfc.nasa.gov/ftp/pro/misc/repstr.pro
and only accepts static expressions, which is how Craig was using it.

** What if you take all spaces out and just put one back in after c **
** and , ?? **

That's OK for that specific string, but I'd like to make it work for
an arbitrary string.

Thanks everyone for their suggestions. --Wayne
Re: Removing some but not all spaces in a string [message #61752 is a reply to message #61750] Tue, 29 July 2008 08:37 Go to previous message
Craig Markwardt is currently offline  Craig Markwardt
Messages: 1869
Registered: November 1996
Senior Member
Bob Crawford <Snowman42@gmail.com> writes:
> On Jul 28, 10:41�pm, Craig Markwardt
> <craigm...@REMOVEcow.physics.wisc.edu> wrote:
>> ... . �It's a pity there isn't a STREPLACE() which
>> handles regular expressions.
>
> Is there a version that handles static replacements?
> If so, why not just replace " > " with ">"; " < " with "<" and " = "
> with "=" etc.?
>
>> 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.
>
> I must be missing something because I'm unfamiliar with REPSTR() as
> well (and my IDL Help has no mention).
> Are these from an external library?

Yep it's in the IDL Astronomy Library.... which Wayne should be
intimately familiar with, but maybe not everybody else :-)
http://idlastro.gsfc.nasa.gov/

Craig

--
------------------------------------------------------------ --------------
Craig B. Markwardt, Ph.D. EMAIL: craigmnet@REMOVEcow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
------------------------------------------------------------ --------------
Re: Removing some but not all spaces in a string [message #61753 is a reply to message #61752] Tue, 29 July 2008 08:33 Go to previous message
humanumbrella is currently offline  humanumbrella
Messages: 52
Registered: June 2008
Member
On Jul 29, 11:27 am, David Fanning <n...@dfanning.com> wrote:
> Craig Markwardt writes:
>>  It's a pity there isn't a STREPLACE() which
>> handles regular expressions.
>
> If I am not mistaken, Mike Galloy has a version
> called STR_REPLACE that handles regular expressions.
> I found it with his white paper on regular expressions
> (a must read, by the way). Don't have a link, but
> certainly it can be found on Mike's web page:
>
>   http://www.michaelgalloy.com/
>
> Cheers,
>
> David
> --
> David Fanning, Ph.D.
> Fanning Software Consulting, Inc.
> Coyote's Guide to IDL Programming:http://www.dfanning.com/
> Sepore ma de ni thui. ("Perhaps thou speakest truth.")

White pages --> http://www.ittvis.com/codebank/search.asp?FID=311
STR_REPLACE from Mike --> http://michaelgalloy.com/2006/06/11/regular-expressions.html
Re: Removing some but not all spaces in a string [message #61754 is a reply to message #61753] Tue, 29 July 2008 08:27 Go to previous message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Craig Markwardt writes:

> It's a pity there isn't a STREPLACE() which
> handles regular expressions.

If I am not mistaken, Mike Galloy has a version
called STR_REPLACE that handles regular expressions.
I found it with his white paper on regular expressions
(a must read, by the way). Don't have a link, but
certainly it can be found on Mike's web page:

http://www.michaelgalloy.com/

Cheers,

David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
Re: Removing some but not all spaces in a string [message #61755 is a reply to message #61754] Tue, 29 July 2008 08:16 Go to previous message
Bob[3] is currently offline  Bob[3]
Messages: 60
Registered: December 2006
Member
On Jul 28, 10:41 pm, Craig Markwardt
<craigm...@REMOVEcow.physics.wisc.edu> wrote:
> ... .  It's a pity there isn't a STREPLACE() which
> handles regular expressions.

Is there a version that handles static replacements?
If so, why not just replace " > " with ">"; " < " with "<" and " = "
with "=" etc.?

> 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.

I must be missing something because I'm unfamiliar with REPSTR() as
well (and my IDL Help has no mention).
Are these from an external library?
Re: Removing some but not all spaces in a string [message #61757 is a reply to message #61755] Tue, 29 July 2008 05:45 Go to previous message
humanumbrella is currently offline  humanumbrella
Messages: 52
Registered: June 2008
Member
On Jul 29, 7:26 am, Jeremy Bailin <astroco...@gmail.com> wrote:
> 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.

What if you take all spaces out and just put one back in after c
and , ??

Cheers,
--Justin
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 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.
Re: Removing some but not all spaces in a string [message #61764 is a reply to message #61758] Mon, 28 July 2008 19:41 Go to previous message
Craig Markwardt is currently offline  Craig Markwardt
Messages: 1869
Registered: November 1996
Senior Member
wlandsman <wlandsman@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: craigmnet@REMOVEcow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
------------------------------------------------------------ --------------
  Switch to threaded view of this topic Create a new topic Submit Reply
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: Wed Oct 08 19:17:31 PDT 2025

Total time taken to generate the page: 0.00858 seconds