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

Home » Public Forums » archive » removing substring from string array
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
removing substring from string array [message #53240] Mon, 02 April 2007 14:21 Go to next message
rchughes is currently offline  rchughes
Messages: 26
Registered: April 2006
Junior Member
Hi All,

Does anyone know a quick way to remove the same prefix from all
elements in a string array?

Here is an example:
start with string_array = ['dog.spot', 'dog.rover', 'dog.lucky']

and get ['spot', 'rover', 'lucky']

The only way I know I can do this is with a FOR loop and the
STRSPLIT(string_array[i], '.', /EXTRACT) command. I am hoping someone
could help me out with a much more efficient way if there is one.

Thanks in advance,
Ryan.
Re: removing substring from string array [message #53321 is a reply to message #53240] Tue, 03 April 2007 07:51 Go to previous messageGo to next message
Ryan. is currently offline  Ryan.
Messages: 77
Registered: March 2006
Member
> Hooray! But, be careful. You don't want to over-design
> the thing. :-)

I like the second method because it also applies to strings that do
not have the prefix. Also, I am only using one prefix but the data
that we have has many different prefixes so it will be easier to apply
if it already in place.

Ryan.
Re: removing substring from string array [message #53322 is a reply to message #53240] Tue, 03 April 2007 08:28 Go to previous message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Ryan. writes:

> But on second thought, I will use the following to be more robust:
>
> string_array =
> ['dog.spot','cat.betsy','doggy.rover','parrot.bill','dog.luc ky']
> p=strpos(string_array,'.')
> print,strmid(string_array,1#p+1)

Hooray! But, be careful. You don't want to over-design
the thing. :-)

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 substring from string array [message #53323 is a reply to message #53240] Tue, 03 April 2007 06:50 Go to previous message
Ryan. is currently offline  Ryan.
Messages: 77
Registered: March 2006
Member
But on second thought, I will use the following to be more robust:

string_array =
['dog.spot','cat.betsy','doggy.rover','parrot.bill','dog.luc ky']
p=strpos(string_array,'.')
print,strmid(string_array,1#p+1)



Ryan.
Re: removing substring from string array [message #53325 is a reply to message #53240] Tue, 03 April 2007 06:21 Go to previous message
Ryan. is currently offline  Ryan.
Messages: 77
Registered: March 2006
Member
> print,strmid(string_array,4)

My current need is only for the same prefix so the above line works
but if that changes I will come back and look at all the responses =)

Thanks everyone for all your help.

Ryan.
Re: removing substring from string array [message #53333 is a reply to message #53240] Mon, 02 April 2007 14:40 Go to previous message
Jean H. is currently offline  Jean H.
Messages: 472
Registered: July 2006
Senior Member
rchughes@gmail.com wrote:
> Hi All,
>
> Does anyone know a quick way to remove the same prefix from all
> elements in a string array?
>
> Here is an example:
> start with string_array = ['dog.spot', 'dog.rover', 'dog.lucky']
>
> and get ['spot', 'rover', 'lucky']
>
> The only way I know I can do this is with a FOR loop and the
> STRSPLIT(string_array[i], '.', /EXTRACT) command. I am hoping someone
> could help me out with a much more efficient way if there is one.
>
> Thanks in advance,
> Ryan.
>

Hi,

If you know that every element in the array starts with the prefix, you
can do something like
prefixLength = strlen(prefix)
stringWithoutPrefix = strmid(stringArray, prefixLength)

If some entries may NOT start with the prefix, you can extract the
prefix, verify it and if it is the good one, remove it

prefix_array = strmid(stringArray, 0, prefixLength)
entriesWithThePrefix = where(prefix_array eq rebin(prefix,
n_elements(stringArray))

stringWithoutPrefix = strmid(stringArray[entriesWithThePrefix],
prefixLength)


You might want to do something to keep the entries that do not start
with the prefix

Hope that helps,
Jean
Re: removing substring from string array [message #53334 is a reply to message #53333] Mon, 02 April 2007 14:44 Go to previous message
Jean H. is currently offline  Jean H.
Messages: 472
Registered: July 2006
Senior Member
> entriesWithThePrefix = where(prefix_array eq rebin(prefix,
> n_elements(stringArray))

Please read entriesWithThePrefix = where(prefix_array eq prefix)
Re: removing substring from string array [message #53335 is a reply to message #53240] Mon, 02 April 2007 14:54 Go to previous message
yp is currently offline  yp
Messages: 42
Registered: February 2005
Member
On Apr 2, 10:21 pm, rchug...@gmail.com wrote:
> Hi All,
>
> Does anyone know a quick way to remove the same prefix from all
> elements in a string array?
>
> Here is an example:
> start with string_array = ['dog.spot', 'dog.rover', 'dog.lucky']
>
> and get ['spot', 'rover', 'lucky']
>
> The only way I know I can do this is with a FOR loop and the
> STRSPLIT(string_array[i], '.', /EXTRACT) command. I am hoping someone
> could help me out with a much more efficient way if there is one.
>
> Thanks in advance,
> Ryan.


string_array = ['dog.spot', 'dog.rover', 'dog.lucky']
tmp=stregex(string_array,'dog.',length=L)
n = 100 ;set max number of characters to be extracted
new_array=transpose((strmid(string_array, L, n))[0,*])

Be cautious when you have a huge string_array...
Re: removing substring from string array [message #53336 is a reply to message #53240] Mon, 02 April 2007 14:38 Go to previous message
JD Smith is currently offline  JD Smith
Messages: 850
Registered: December 1999
Senior Member
On Mon, 02 Apr 2007 14:21:42 -0700, rchughes wrote:

> Hi All,
>
> Does anyone know a quick way to remove the same prefix from all
> elements in a string array?
>
> Here is an example:
> start with string_array = ['dog.spot', 'dog.rover', 'dog.lucky']
>
> and get ['spot', 'rover', 'lucky']
>
> The only way I know I can do this is with a FOR loop and the
> STRSPLIT(string_array[i], '.', /EXTRACT) command. I am hoping someone
> could help me out with a much more efficient way if there is one.

For a fixed length string like this, it's easy:

print,strmid(string_array,4)

But, suppose it's a non-uniform length string:

string_array = ['dog.spot','cat.betsy','doggy.rover','parrot.bill','dog.luc ky']
p=strpos(string_array,'.')
print,strmid(string_array,1#p+1)

Notice that silly use of 1#p. This is to ensure that only 1
sub-string is extracted from each string in the array (try it without
the 1# and you'll see what I mean; see STRMID docs).

Or you could even be more general and use a regular expression:

string_array = ['dog.spot','cat*betsy','doggy#rover','parrot&bill','dog @lucky']
p=stregex(string_array,'^.+[.*#&@]',LENGTH=len)
print,strmid(string_array,1#len)

JD
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: rotate and transpose images in xobjviewer
Next Topic: General help

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

Current Time: Wed Oct 08 14:00:00 PDT 2025

Total time taken to generate the page: 0.00427 seconds