Re: sorting the chars in a string [message #58631] |
Mon, 11 February 2008 08:23 |
Spon
Messages: 178 Registered: September 2007
|
Senior Member |
|
|
elwood wrote:
> Excellent!
> Thank you!
> A minor variation on the below-
> what if I want to split a string into a string array in which each
> element is one character?
> I think I can figure out a lengthy way to do it if I first convert the
> string to byte.
> BUT
> strjoin will knit a string array back together, but is there a way to
> use strsplit to rip off each char,
> one by one and put them into a string array?
> Something like the following perhaps;
> x="abdcf"
> arrayofchars=strsplit(x,'.',/regex,/extract)
>
> -the above doesnt work, it returns an empty string :-(
new_string_array = string(transpose(tmp[sort_inds]))
>
> On Feb 11, 9:02 am, Lasse Clausen <la...@lbnc.de> wrote:
>> On 11 Feb, 15:39, elwood <epolo...@uwsp.edu> wrote:
>>
>>> I've been trying to think of a simple way to alphabetically sort an
>>> individual string
>>
>>> Say I have the string x="abdcf"
>>> Is there a way to sort it and return a new string which is in correct
>>> alphabetical order,
>>> and/or reverse alphabetical order.
>>
>>> Maybe its obvious, but I havent worked with strings too much in IDL
>>> and I cant seem to find
>>> a way to do this (strsplit and stregex somehow??)
>>
>>> Thanks,
>>> Elisha
>>
>> Hi there,
>>
>> convert the string to an array of bytes, like so
>>
>> test = 'sfbvvaedfvtrgvsdvrbnyhtfc'
>> tmp = byte(test)
>>
>> This conversion happens according to the ASCII table, if I am not
>> mistanken, which connects a number to a character in ascending order,
>> on my machine a = 97, b = 98 and so on. Then sort that byte array and
>> build up the string again.
>>
>> sort_inds = sort(tmp)
>> new_string = string(tmp[sort_inds])
>> print, new_string
>>
>> That works, use REVERSE() if you want the sorting done from z-a.
>>
>> Cheers
>> Lasse Clausen
|
|
|
Re: sorting the chars in a string [message #58632 is a reply to message #58631] |
Mon, 11 February 2008 07:37  |
Foldy Lajos
Messages: 268 Registered: October 2001
|
Senior Member |
|
|
On Mon, 11 Feb 2008, elwood wrote:
>
>
> I've been trying to think of a simple way to alphabetically sort an
> individual string
>
> Say I have the string x="abdcf"
> Is there a way to sort it and return a new string which is in correct
> alphabetical order,
> and/or reverse alphabetical order.
>
> Maybe its obvious, but I havent worked with strings too much in IDL
> and I cant seem to find
> a way to do this (strsplit and stregex somehow??)
>
> Thanks,
> Elisha
>
Just for fun, you can do it without an explicit sort:
b=byte(x)
h=histogram(b, rev=r)
x=string(b[r[n_elements(r)-n_elements(b):*]])
or
x=string(b[reverse(r[n_elements(r)-n_elements(b):*])])
regards,
lajos
|
|
|
Re: sorting the chars in a string [message #58633 is a reply to message #58632] |
Mon, 11 February 2008 07:36  |
elwood
Messages: 23 Registered: February 2007
|
Junior Member |
|
|
Excellent!
Thank you!
A minor variation on the below-
what if I want to split a string into a string array in which each
element is one character?
I think I can figure out a lengthy way to do it if I first convert the
string to byte.
BUT
strjoin will knit a string array back together, but is there a way to
use strsplit to rip off each char,
one by one and put them into a string array?
Something like the following perhaps;
x="abdcf"
arrayofchars=strsplit(x,'.',/regex,/extract)
-the above doesnt work, it returns an empty string :-(
On Feb 11, 9:02 am, Lasse Clausen <la...@lbnc.de> wrote:
> On 11 Feb, 15:39, elwood <epolo...@uwsp.edu> wrote:
>
>> I've been trying to think of a simple way to alphabetically sort an
>> individual string
>
>> Say I have the string x="abdcf"
>> Is there a way to sort it and return a new string which is in correct
>> alphabetical order,
>> and/or reverse alphabetical order.
>
>> Maybe its obvious, but I havent worked with strings too much in IDL
>> and I cant seem to find
>> a way to do this (strsplit and stregex somehow??)
>
>> Thanks,
>> Elisha
>
> Hi there,
>
> convert the string to an array of bytes, like so
>
> test = 'sfbvvaedfvtrgvsdvrbnyhtfc'
> tmp = byte(test)
>
> This conversion happens according to the ASCII table, if I am not
> mistanken, which connects a number to a character in ascending order,
> on my machine a = 97, b = 98 and so on. Then sort that byte array and
> build up the string again.
>
> sort_inds = sort(tmp)
> new_string = string(tmp[sort_inds])
> print, new_string
>
> That works, use REVERSE() if you want the sorting done from z-a.
>
> Cheers
> Lasse Clausen
|
|
|
Re: sorting the chars in a string [message #58634 is a reply to message #58633] |
Mon, 11 February 2008 07:02  |
lasse
Messages: 48 Registered: February 2007
|
Member |
|
|
On 11 Feb, 15:39, elwood <epolo...@uwsp.edu> wrote:
> I've been trying to think of a simple way to alphabetically sort an
> individual string
>
> Say I have the string x="abdcf"
> Is there a way to sort it and return a new string which is in correct
> alphabetical order,
> and/or reverse alphabetical order.
>
> Maybe its obvious, but I havent worked with strings too much in IDL
> and I cant seem to find
> a way to do this (strsplit and stregex somehow??)
>
> Thanks,
> Elisha
Hi there,
convert the string to an array of bytes, like so
test = 'sfbvvaedfvtrgvsdvrbnyhtfc'
tmp = byte(test)
This conversion happens according to the ASCII table, if I am not
mistanken, which connects a number to a character in ascending order,
on my machine a = 97, b = 98 and so on. Then sort that byte array and
build up the string again.
sort_inds = sort(tmp)
new_string = string(tmp[sort_inds])
print, new_string
That works, use REVERSE() if you want the sorting done from z-a.
Cheers
Lasse Clausen
|
|
|