Array of strings- a macro question

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

Array of strings- a macro question

GuyM
Hello everyone,
I am writing a macro that will accept from the user string arguments, in order to sort files in one folder into different subfolders, based on differences in string occurrences in these file names (i.e. "...RedChannel..." vs "...BlueChannel..."). But I require some assistance with the following (displayed are only pieces of the code):

The user inputs the number of desired inputs/strings (manifested here below as "myString"):
    for (j=0; j<MyString; j++) {
    Dialog.addString("String "+j, title);
    }
    Dialog.show();


and then inputs the same number of strings to serve as future substring regexp and to construct the subfolders names:
    for (j=0; j<MyString; j++) {
    CurrentString= Dialog.getString();
    StringArray=newArray(MyString);
    FolderListPath=newArray(MyString);
    StringArray[j]=CurrentString;
    CurrentPath = path +CurrentString+ File.separator;
    FolderListPath[j]=CurrentPath;
    File.makeDirectory(CurrentPath);
    }


But I noticed something strange: as the loop (the second one) proceeds, the arguments are passed ok one by one into the arrays. Once the loop terminates, the last row of the arrays are ok, but the rows above it are filled with zeros! I checked, and this also happens when numeric variables are being input instead of strings.

Any idea?
Thanks a lot in advance and all the best!
Guy

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Array of strings- a macro question

George Patterson
Guy,
I think you need to move the following lines outside the loop before the "for
(j=0; j<MyString; j++)".
StringArray=newArray(MyString);
FolderListPath=newArray(MyString);
These are making a new array each time through the for loop and you end up
with only the last one containing the last string.
I think you just need to create those arrays one time for that part of the
code at least.
Best,
George

On Tue, Aug 4, 2015 at 5:01 PM, GuyM <[hidden email]>
wrote:

> Hello everyone,
> I am writing a macro that will accept from the user string arguments, in
> order to sort files in one folder into different subfolders, based on
> differences in string occurrences in these file names (i.e.
> "...RedChannel..." vs "...BlueChannel..."). But I require some assistance
> with the following (displayed are only pieces of the code):
>
> The user inputs the number of desired inputs/strings (manifested here
> below as "myString"):
>     for (j=0; j<MyString; j++) {
>     Dialog.addString("String "+j, title);
>     }
>     Dialog.show();
>
>
> and then inputs the same number of strings to serve as future substring
> regexp and to construct the subfolders names:
>     for (j=0; j<MyString; j++) {
>     CurrentString= Dialog.getString();
>     StringArray=newArray(MyString);
>     FolderListPath=newArray(MyString);
>     StringArray[j]=CurrentString;
>     CurrentPath = path +CurrentString+ File.separator;
>     FolderListPath[j]=CurrentPath;
>     File.makeDirectory(CurrentPath);
>     }
>
>
> But I noticed something strange: as the loop (the second one) proceeds,
> the arguments are passed ok one by one into the arrays. Once the loop
> terminates, the last row of the arrays are ok, but the rows above it are
> filled with zeros! I checked, and this also happens when numeric variables
> are being input instead of strings.
>
> Any idea?
> Thanks a lot in advance and all the best!
> Guy
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Array of strings- a macro question

GuyM
In reply to this post by GuyM
Hello George,
Thanks a lot!
Would you also happen to know if there is a "string compare" function in the macro language? For the meantime I'm working around this by using "indexOf", i.e. to see when its value is not false.
Best wishes
Guy

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Array of strings- a macro question

Straub, Volko A. (Dr.)
All macro functions are documented here:
http://imagej.nih.gov/ij/developer/macro/functions.html#S.
In addition to indexOf(), startsWith() and endsWith() might be useful if
you try to identify strings with specific patterns.
Hope this helps,
Volko

On 05/08/2015 06:59, GuyM wrote:
> Hello George,
> Thanks a lot!
> Would you also happen to know if there is a "string compare" function in the macro language? For the meantime I'm working around this by using "indexOf", i.e. to see when its value is not false.
> Best wishes
> Guy
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Array of strings- a macro question

GuyM
In reply to this post by GuyM
Hey Volko,
Thanks.
Yes, I've seen these functions, but they don't seem to be helpful for my case (as I'm looking to work with substring occurences in the middle of the original string).
All the best
Guy

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Array of strings- a macro question

George Patterson
Hi Guy,
The matches(string, regex) function is probably what you want to compare
two strings, but it sounds like you want to search for a substring within a
larger string.
I've pasted a little function below which I've used to search for a string
inside a longer string.  There might be a function already in the macro
language to do this, but I wasn't able to find it at the time I assembled
the script below.  It's pretty crude, but seemed to get the job done for
me.  You might be able to modify it for your use.  If you find a bug or
more likely many bugs please let me know.
Best,
George

function checkForMatches(fragSeq,checkSeq){
numMatchesFound=0;
//print("fragSeq="+fragSeq+"   checkSeq="+checkSeq);
if(lengthOf(fragSeq)>lengthOf(checkSeq)){
return false;
}
for(i=0;i<=lengthOf(checkSeq)-lengthOf(fragSeq);i++){
if(matches(fragSeq,substring(checkSeq,i,i+lengthOf(fragSeq)))){
numMatchesFound++;
}
}
print("numMatchesFound="+numMatchesFound);
if(numMatchesFound>0){
return true;
}else{
return false;
}
}

On Wed, Aug 5, 2015 at 3:35 AM, Guy Malkinson <
[hidden email]> wrote:

> Hey Volko,
> Thanks.
> Yes, I've seen these functions, but they don't seem to be helpful for my
> case (as I'm looking to work with substring occurences in the middle of the
> original string).
> All the best
> Guy
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Array of strings- a macro question

GuyM
In reply to this post by GuyM
Hello George,
Thanks for the code and for the assistance; for the meantime I've managed to work around this issue by using indexOf: when its value is not -1, this is indication that there is a match between regexp and string, and the file is saved into the appropriate subfolder.

I've appended below the code I wrote, for the sake of the greater mailing list community. Feel free to comment and change as needed.

The code is intended for the situation where many files of different properties/modalities (i.e. channels, timepoints etc) are found in a single folder and the user would like to separate some/all of them into subfolders, based on unique string sequences that appear in their names. There is no limit on the number of differentiating strings. It is currently set to work on tiff files.
All the best
Guy

path = getDirectory("Choose a directory");
filename = getFileList(path);
MyString=getNumber("Into how many different subfolders would you like to sort the files?", 2);

Dialog.create("Enter names of folders into which files will be subdivided");
Dialog.addMessage("Enter strings as names of folders into which files will be subdivided. \n Names are based on different strings (eg channels, timepoints) that appear in the files names.");
title="None";

for (j=0; j<MyString; j++) {
Dialog.addString("String "+j, title);
}

Dialog.show();

StringArray=newArray(MyString);
FolderListPath=newArray(MyString);
for (j=0; j<MyString; j++) {
CurrentString= Dialog.getString();                          

StringArray[j]=CurrentString;
CurrentPath = path +CurrentString+ File.separator;
FolderListPath[j]=CurrentPath;

File.makeDirectory(CurrentPath);
}

setBatchMode(true);

for (i=0; i<filename.length; i++) {
        if(endsWith(filename[i], ".tif")) {
                open(path+filename[i]);
name=File.nameWithoutExtension;

for (checkIndex=0;checkIndex<MyString; checkIndex++) {

indexOfCh=indexOf(name, StringArray[checkIndex]);
if (indexOfCh != -1) {
saveAs("tiff", FolderListPath[checkIndex] + getTitle);
}
}
close();
        }
}

"DONE"

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html