split string macro function

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

split string macro function

lechristophe
I'm trying to split a string (obtained from reading a text file) using the
split(string,delimiters) macro function :

*split(string, delimiters)*
Breaks a string into an array of substrings. *Delimiters* is a string
containing one or more delimiter characters. The default delimiter set "
\t\n\r" (space, tab, newline, return) is used if *delimiters* is an empty
string or split is called with only one argument. Returns a one element
array if no delimiter is found.

However I don't understand the output of the function if I use a complex
delimiter such as "// part". See this exemple macro :

print("\\Clear");
string="// test\n// part 1\na\nb\nc\n// part 2\na\nb\nc\n// end";

print("string=\n"+string);
print("\n");

parts1=split(string, "//");
for(i=0; i<parts1.length; i++) {
print("parts1["+i+"]="+parts1[i]);
}
print("\n");

parts2=split(string, "// part");
for(i=0; i<parts2.length; i++) {
print("parts2["+i+"]="+parts2[i]);
}

the output of the first split (parts1) is OK. However I don't understand the
output of the second split (parts2). I've tried using brackets "[// part]"
like in file paths but it doesn't change anything. "/" is not a special
character also. Could anyone explain what happens here ?

Thank you for your help,



--
Christophe Leterrier
Postdoc
INSERM UMR641 // Ionic channels Lab
IFR Jean Roche, Mediterranée University
Marseille, France
[hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: split string macro function

lechristophe
It seems there is something wrong with the split() fucntion. Consider this
macro :

print("\\Clear");
string="test part 1 part 2 end"
string2="test // 1 // 2 end"
print("string1="+string);

delimiter1="part";
print("delimiter1="+delimiter1);

parts=split(string, delimiter1);
for(i=0; i<parts.length; i++) {
 print("parts["+i+"]="+parts[i]);
}
print("\n");

print("string2="+string2);

delimiter2="//";
print("delimiter2="+delimiter2);

parts=split(string2, delimiter2);
for(i=0; i<parts.length; i++) {
print("parts["+i+"]="+parts[i]);
}
print("\n");


the output is :

string1=test part 1 part 2 end
delimiter1=part
parts[0]=es
parts[1]=
parts[2]= 1
parts[3]= 2 end

string2=test // 1 // 2 end
delimiter2=//
parts[0]=test
parts[1]= 1
parts[2]= 2 end


so string1 is not correctly split by the delimiter "part" ?

Christophe



On Wed, Sep 22, 2010 at 15:30, Christophe Leterrier <
[hidden email]> wrote:

> I'm trying to split a string (obtained from reading a text file) using the
> split(string,delimiters) macro function :
>
> *split(string, delimiters)*
> Breaks a string into an array of substrings. *Delimiters* is a string
> containing one or more delimiter characters. The default delimiter set "
> \t\n\r" (space, tab, newline, return) is used if *delimiters* is an empty
> string or split is called with only one argument. Returns a one element
> array if no delimiter is found.
>
> However I don't understand the output of the function if I use a complex
> delimiter such as "// part". See this exemple macro :
>
> print("\\Clear");
> string="// test\n// part 1\na\nb\nc\n// part 2\na\nb\nc\n// end";
>
> print("string=\n"+string);
> print("\n");
>
> parts1=split(string, "//");
> for(i=0; i<parts1.length; i++) {
> print("parts1["+i+"]="+parts1[i]);
> }
> print("\n");
>
> parts2=split(string, "// part");
> for(i=0; i<parts2.length; i++) {
> print("parts2["+i+"]="+parts2[i]);
> }
>
> the output of the first split (parts1) is OK. However I don't understand
> the output of the second split (parts2). I've tried using brackets "[//
> part]" like in file paths but it doesn't change anything. "/" is not a
> special character also. Could anyone explain what happens here ?
>
> Thank you for your help,
>
>
>
> --
> Christophe Leterrier
> Postdoc
> INSERM UMR641 // Ionic channels Lab
> IFR Jean Roche, Mediterranée University
> Marseille, France
> [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|

Re: split string macro function

Michael Schmid
In reply to this post by lechristophe
Hi Christophe,

in contrast to Java's String.split, the macro split function splits  
uses every character in the second string as a possible delimiter.

For the code used, see the split(String str, String delim) method in

http://pacific.mpi-cbg.de/cgi-bin/gitweb.cgi?p=imagej.git;a=blob;f=ij/ 
util/Tools.java

(the link should be one line)


Michael

________________________________________________________________

On 22 Sep 2010, at 15:30, Christophe Leterrier wrote:

> print("\\Clear");
> string="// test\n// part 1\na\nb\nc\n// part 2\na\nb\nc\n// end";
>
> print("string=\n"+string);
> print("\n");
>
> parts1=split(string, "//");
> for(i=0; i<parts1.length; i++) {
> print("parts1["+i+"]="+parts1[i]);
> }
> print("\n");
>
> parts2=split(string, "// part");
> for(i=0; i<parts2.length; i++) {
> print("parts2["+i+"]="+parts2[i]);
> }
Reply | Threaded
Open this post in threaded view
|

Re: split string macro function

lechristophe
Thanks Michael for the info.

I came to this same conclusion testing different delimiters... However this
is not clear from the description of the split() function in the"built-in
macro functions" web page. Moreover, it doesn't make a lot of sense, right ?
Would it be possible to modify the function or to add something like a
"wordsplit" function that would use an arbitrary string as a whole for its
delimiter ?

On Wed, Sep 22, 2010 at 16:00, Michael Schmid <[hidden email]>wrote:

> Hi Christophe,
>
> in contrast to Java's String.split, the macro split function splits uses
> every character in the second string as a possible delimiter.
>
> For the code used, see the split(String str, String delim) method in
>
>
> http://pacific.mpi-cbg.de/cgi-bin/gitweb.cgi?p=imagej.git;a=blob;f=ij/util/Tools.java
>
> (the link should be one line)
>
>
> Michael
>
> ________________________________________________________________
>
>
> On 22 Sep 2010, at 15:30, Christophe Leterrier wrote:
>
>  print("\\Clear");
>> string="// test\n// part 1\na\nb\nc\n// part 2\na\nb\nc\n// end";
>>
>> print("string=\n"+string);
>> print("\n");
>>
>> parts1=split(string, "//");
>> for(i=0; i<parts1.length; i++) {
>> print("parts1["+i+"]="+parts1[i]);
>> }
>> print("\n");
>>
>> parts2=split(string, "// part");
>> for(i=0; i<parts2.length; i++) {
>> print("parts2["+i+"]="+parts2[i]);
>> }
>>
>
Reply | Threaded
Open this post in threaded view
|

Re: split string macro function

Mario Faretta
  I don't know if it can help you but I successfully used "nested"
delimiters using single characters (*|a|b|c... *|a1|b1|c1... splitted
before according to "\*" and then to "\|" gave correct results)
Hope it helps
Mario

Il 9/22/2010 4:20 PM, Christophe Leterrier ha scritto:

> Thanks Michael for the info.
>
> I came to this same conclusion testing different delimiters... However this
> is not clear from the description of the split() function in the"built-in
> macro functions" web page. Moreover, it doesn't make a lot of sense, right ?
> Would it be possible to modify the function or to add something like a
> "wordsplit" function that would use an arbitrary string as a whole for its
> delimiter ?
>
> On Wed, Sep 22, 2010 at 16:00, Michael Schmid<[hidden email]>wrote:
>
>> Hi Christophe,
>>
>> in contrast to Java's String.split, the macro split function splits uses
>> every character in the second string as a possible delimiter.
>>
>> For the code used, see the split(String str, String delim) method in
>>
>>
>> http://pacific.mpi-cbg.de/cgi-bin/gitweb.cgi?p=imagej.git;a=blob;f=ij/util/Tools.java
>>
>> (the link should be one line)
>>
>>
>> Michael
>>
>> ________________________________________________________________
>>
>>
>> On 22 Sep 2010, at 15:30, Christophe Leterrier wrote:
>>
>>   print("\\Clear");
>>> string="// test\n// part 1\na\nb\nc\n// part 2\na\nb\nc\n// end";
>>>
>>> print("string=\n"+string);
>>> print("\n");
>>>
>>> parts1=split(string, "//");
>>> for(i=0; i<parts1.length; i++) {
>>> print("parts1["+i+"]="+parts1[i]);
>>> }
>>> print("\n");
>>>
>>> parts2=split(string, "// part");
>>> for(i=0; i<parts2.length; i++) {
>>> print("parts2["+i+"]="+parts2[i]);
>>> }
>>>


--
---PLEASE Note the change in telephone number---

--
Mario Faretta
Department of Experimental Oncology
European Institute of Oncology
c/o IFOM-IEO Campus for Oncogenomics
via Adamello 16 20139 Milan Italy
Phone: ++39-0294375027
email: [hidden email]
http://www.ifom-ieo-campus.it
Reply | Threaded
Open this post in threaded view
|

Re: split string macro function

Rasband, Wayne (NIH/NIMH) [E]
In reply to this post by lechristophe
You can split a string using a word delimiter by using the replace() function to replace the word with a single unique character and then using that character as a delimiter. Here is an example:

  print("\\Clear");
  string="test part 1 part 2 end"
  print("string="+string);
  delimiter=" part ";
  print("delimiter=\""+delimiter+"\"");
  string2 = replace(string, delimiter, "@");
  print("string2="+string2);
  parts = split(string2, "@");
  for(i=0; i<parts.length; i++)
     print("parts["+i+"]="+parts[i]);

-wayne

On Sep 22, 2010, at 9:55 AM, Christophe Leterrier wrote:

> It seems there is something wrong with the split() fucntion. Consider this
> macro :
>
> print("\\Clear");
> string="test part 1 part 2 end"
> string2="test // 1 // 2 end"
> print("string1="+string);
>
> delimiter1="part";
> print("delimiter1="+delimiter1);
>
> parts=split(string, delimiter1);
> for(i=0; i<parts.length; i++) {
> print("parts["+i+"]="+parts[i]);
> }
> print("\n");
>
> print("string2="+string2);
>
> delimiter2="//";
> print("delimiter2="+delimiter2);
>
> parts=split(string2, delimiter2);
> for(i=0; i<parts.length; i++) {
> print("parts["+i+"]="+parts[i]);
> }
> print("\n");
>
>
> the output is :
>
> string1=test part 1 part 2 end
> delimiter1=part
> parts[0]=es
> parts[1]=
> parts[2]= 1
> parts[3]= 2 end
>
> string2=test // 1 // 2 end
> delimiter2=//
> parts[0]=test
> parts[1]= 1
> parts[2]= 2 end
>
>
> so string1 is not correctly split by the delimiter "part" ?
>
> Christophe
>
>
>
> On Wed, Sep 22, 2010 at 15:30, Christophe Leterrier <
> [hidden email]> wrote:
>
>> I'm trying to split a string (obtained from reading a text file) using the
>> split(string,delimiters) macro function :
>>
>> *split(string, delimiters)*
>> Breaks a string into an array of substrings. *Delimiters* is a string
>> containing one or more delimiter characters. The default delimiter set "
>> \t\n\r" (space, tab, newline, return) is used if *delimiters* is an empty
>> string or split is called with only one argument. Returns a one element
>> array if no delimiter is found.
>>
>> However I don't understand the output of the function if I use a complex
>> delimiter such as "// part". See this exemple macro :
>>
>> print("\\Clear");
>> string="// test\n// part 1\na\nb\nc\n// part 2\na\nb\nc\n// end";
>>
>> print("string=\n"+string);
>> print("\n");
>>
>> parts1=split(string, "//");
>> for(i=0; i<parts1.length; i++) {
>> print("parts1["+i+"]="+parts1[i]);
>> }
>> print("\n");
>>
>> parts2=split(string, "// part");
>> for(i=0; i<parts2.length; i++) {
>> print("parts2["+i+"]="+parts2[i]);
>> }
>>
>> the output of the first split (parts1) is OK. However I don't understand
>> the output of the second split (parts2). I've tried using brackets "[//
>> part]" like in file paths but it doesn't change anything. "/" is not a
>> special character also. Could anyone explain what happens here ?
>>
>> Thank you for your help,
>>
>>
>>
>> --
>> Christophe Leterrier
>> Postdoc
>> INSERM UMR641 // Ionic channels Lab
>> IFR Jean Roche, Mediterranée University
>> Marseille, France
>> [hidden email]
>>
>>