Dear ImageJ users
I think I found a problem in "lastIndexOf" If I run the macro code: ArrayId = "123test"; Id = "123"; index = lastIndexOf(ArrayId, Id); print(ArrayId+" "+Id+" "+index); I would expect index to be 4 (or 3?) but instead I get 0. Is this a bug or do I misunderstand the use of lastIndexOf? Best wishes Kees -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
lastIndexOf() returns the index to the beginning of the last matching string, so zero is the correct answer. You have only 1 instance of the matching string '123'.
if you had '123test123' then lastIndexOf('123') would return 7 On Mar 25, 2013, at 11:57 AM, Straatman, Kees R. (Dr.) wrote: > Dear ImageJ users > > I think I found a problem in "lastIndexOf" > > If I run the macro code: > > ArrayId = "123test"; > Id = "123"; > index = lastIndexOf(ArrayId, Id); > print(ArrayId+" "+Id+" "+index); > > I would expect index to be 4 (or 3?) but instead I get 0. > > Is this a bug or do I misunderstand the use of lastIndexOf? > > Best wishes > > Kees > > > > -- > ImageJ mailing list: http://imagej.nih.gov/ij/list.html Walter O'Dell, PhD Assistant Professor Dept. Radiation Oncology McKnight Brain Inst. Rm LG-177 PO Box 100385 Shands Cancer Center, University of Florida Gainesville, FL 32610 [hidden email] 352-273-9030 http://odell.radonc.med.ufl.edu ___________ -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
In reply to this post by Krs5
Hi,
On Mar 25, 2013, at 11:57 AM, Straatman, Kees R. (Dr.) wrote: > Dear ImageJ users > > I think I found a problem in "lastIndexOf" > > If I run the macro code: > > ArrayId = "123test"; > Id = "123"; > index = lastIndexOf(ArrayId, Id); > print(ArrayId+" "+Id+" "+index); > > I would expect index to be 4 (or 3?) but instead I get 0. > > Is this a bug or do I misunderstand the use of lastIndexOf? > It's the index of the first character of the the substring '123' So, of you prepend with more characters, the index return still reveals the location of the "1" in "123". ArrayId = "abc123test"; Id = "123"; index = lastIndexOf(ArrayId, Id); print(ArrayId+" "+Id+" "+index); log> abc123test 123 3 I can see how the "rightmost" part of the docs might be a bit confusing at first. You can test this idea by repeating the substring in the target string. It reports the the location of the "1" in the right hand substring. ArrayId = "abc123test123xyz"; Id = "123"; index = lastIndexOf(ArrayId, Id); print(ArrayId+" "+Id+" "+index); log> abc123test123xyz 123 10 Cheers, Ben > Best wishes > > Kees > > > > -- > ImageJ mailing list: http://imagej.nih.gov/ij/list.html -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Thanks for the replays. Yes, I was confused by the documentation.
Best wishes Kees -----Original Message----- From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of Ben Tupper Sent: 25 March 2013 16:38 To: [hidden email] Subject: Re: lastIndexOf Hi, On Mar 25, 2013, at 11:57 AM, Straatman, Kees R. (Dr.) wrote: > Dear ImageJ users > > I think I found a problem in "lastIndexOf" > > If I run the macro code: > > ArrayId = "123test"; > Id = "123"; > index = lastIndexOf(ArrayId, Id); > print(ArrayId+" "+Id+" "+index); > > I would expect index to be 4 (or 3?) but instead I get 0. > > Is this a bug or do I misunderstand the use of lastIndexOf? > It's the index of the first character of the the substring '123' So, of you prepend with more characters, the index return still reveals the location of the "1" in "123". ArrayId = "abc123test"; Id = "123"; index = lastIndexOf(ArrayId, Id); print(ArrayId+" "+Id+" "+index); log> abc123test 123 3 I can see how the "rightmost" part of the docs might be a bit confusing at first. You can test this idea by repeating the substring in the target string. It reports the the location of the "1" in the right hand substring. ArrayId = "abc123test123xyz"; Id = "123"; index = lastIndexOf(ArrayId, Id); print(ArrayId+" "+Id+" "+index); log> abc123test123xyz 123 10 Cheers, Ben > Best wishes > > Kees > > > > -- > ImageJ mailing list: http://imagej.nih.gov/ij/list.html -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Free forum by Nabble | Edit this page |