Skip to main content
Inspiring
March 21, 2012
Answered

Dynamic If Statment

  • March 21, 2012
  • 1 reply
  • 1138 views

Is it possible to create an if statment with dynamic logical tests?  For example, I have a .csv file that has a file name in the first column, and the columns after that have random tag data.  The first row of the .csv is a header (so it's pretty much just ignored), the second row is the file and tags to be compared to the others, the third row is the header for the file name column and all of the tag category columns after that, and below that are all the files and their tags to be compare row 2 to.  The problem is, not every category of files has the same number of tags, and not all tags may be necessary for the comparison, so I was going to have a dialog window of checkboxes come up listing the header row's non-null tag columns as individual checkboxes.  The if statement would then need to compare all of the selected columns to the other listed files (the on row 4 and below) to find any that contain the exact same tags as the source file (the one in row 2).

Below is kind of a rough mock-up of what I'm trying to do.

var baseTag = [];

baseTag = data[1].split(','); //the comparison row's columns loaded to the baseTag array

if(checkbox1.value) tag1 = true;

if(checkbox2.value) tag2 = true;

if(checkbox3.value) tag3 = true;

///etc. etc.///

//check for any files that match

for(i=3;i<data.length;i++) { //data being the array containing all the rows of the .csv

     var tag = [];

     tag = data.split(',');

     if(baseTag[1] == tag[1] && baseTag[2] == tag[2] && baseTag[3] == tag[3] /*etc*/) { //this needs to be set up so that if tag2 = false, it doesn't do the "baseTag[2] == tag[2]" check; but if it's true, it does check it

          open(tag[0]);

     }

}

Anyway, any ideas on how to do this?  I have the feeling it's something simple, but just can't seem to figure it out.  Thanks in advance for any help!

dgolberg

This topic has been closed for replies.
Correct answer Paul Riggott

Hey Michael, thanks for the reply.  The array labelled "baseTag" holds the data for a single row in a .csv file (for simplicity, we'll say it's the very first row).  The array labelled "tag" holds the data for all the remaining rows of the .csv file.  So for example, the .csv might look something like below when opened with a spreadsheet program:

File NameTag1Tag2Tag3
FileName1.formatwooddarkstrong
FileName2.formatwoodlightstrong
FileName3.formatwoodmediumlight
FileName4.formatwooddarkstrong

FileName1.format is the file we're doing the check on using the tag columns to the right, and if it finds a match using those tags, it opens the file listed in column 1.  So the script would need to make sure all tags match the first row's tags (in this example, only FileName4.format would match FileName1.format's tags).  However, there may be instances where I wish to exclude one of the columns from the check (for example, only check for matches of tag 1 and 3).  So this would require that the if statement's check only look for matches of the tag1 and tag3 columns while ignoring Tag2 (so now FileName2.format would also match).  Essentially, instead of being:

if(baseTag[1] == tag[1] && baseTag[2] == tag[2] && baseTag[3] == tag[3]) {
          open(tag[0]);
     }

it would then be:

if(baseTag[1] == tag[1] && baseTag[3] == tag[3]) {
          open(tag[0]);
     }

if I decide to have it exclude the tag2 column.

The issue I'm having is making this change to the if statement be dynamic (without a huge mess of inefficient code).  Your mention of a switch statement sounds interesting.  I'm not the greatest programmer yet (pretty much just the really basic stuff) so I hadn't heard about this statement yet; but I'll certainly be looking it up now.  Anyway, hope the info helps clarify it a little better.


Would something like this work?

var w = new Window('dialog','tag test');
w.cb1 = w.add('checkbox',undefined,'Use Tag 1');
w.cb2 = w.add('checkbox',undefined,'Use Tag 2');
w.cb3 = w.add('checkbox',undefined,'Use Tag 3');
w.bu1 = w.add('button',undefined,'Cancel');
w.bu2 = w.add('button',undefined,'Process');
w.bu2.onClick=function(){
w.close(1);
var csvFile = File.openDialog("Open Comma-delimited File","comma-delimited(*.csv):*.csv;");
csvFile.open('r');
var Data = csvFile.read();
csvFile.close();
Data = Data.split('\n');
Data.shift(); //remove header line
//alert(Data[0]);
var maintag = Data.shift().split(',');
var string1 ='';
if(w.cb1.value) string1 += maintag[1].toString().replace(/^\s+|\s+$/g,'');
if(w.cb2.value) string1 += maintag[2].toString().replace(/^\s+|\s+$/g,'');
if(w.cb3.value) string1 += maintag[3].toString().replace(/^\s+|\s+$/g,'');
//alert(string1);
while(Data.length>1){
var sectag = Data.shift().split(',');
var string2 ='';
if(w.cb1.value) string2 += sectag[1].toString().replace(/^\s+|\s+$/g,'');
if(w.cb2.value) string2 += sectag[2].toString().replace(/^\s+|\s+$/g,'');
if(w.cb3.value) string2 += sectag[3].toString().replace(/^\s+|\s+$/g,'');
if(string1 == string2) $.writeln(sectag[0].toString());   
    }//end while
}
w.show();

/**************CSV file.*****************************
File Name,Tag1,Tag2,Tag3
FileName1.format,wood ,dark,strong
FileName2.format,wood,light, strong
FileName3.format,wood,medium, light
FileName4.format,wood, dark  , strong
*****************************************************/

1 reply

dgolbergAuthor
Inspiring
March 23, 2012

Seems I've come up with a stumper.  I'm not even sure if this is doable, but if anyone has any ideas or knows if it's doable or not; let me know.  The script basically just needs to check for files whose tags match the first file's tags and open them in Photoshop so we can compare and get rid of any duplicates.  The biggest obstacle being the random number of tag categories per file category.

Inspiring
March 23, 2012

I think you have way too many conditions to check with just one if statement. My guess is that you will need multiple if statements and it may also help to have a switch statement.

It's hard to make a suggestion to the logic flow you will need because I an not clear what data those arrays hold.

dgolbergAuthor
Inspiring
March 23, 2012

Hey Michael, thanks for the reply.  The array labelled "baseTag" holds the data for a single row in a .csv file (for simplicity, we'll say it's the very first row).  The array labelled "tag" holds the data for all the remaining rows of the .csv file.  So for example, the .csv might look something like below when opened with a spreadsheet program:

File NameTag1Tag2Tag3
FileName1.formatwooddarkstrong
FileName2.formatwoodlightstrong
FileName3.formatwoodmediumlight
FileName4.formatwooddarkstrong

FileName1.format is the file we're doing the check on using the tag columns to the right, and if it finds a match using those tags, it opens the file listed in column 1.  So the script would need to make sure all tags match the first row's tags (in this example, only FileName4.format would match FileName1.format's tags).  However, there may be instances where I wish to exclude one of the columns from the check (for example, only check for matches of tag 1 and 3).  So this would require that the if statement's check only look for matches of the tag1 and tag3 columns while ignoring Tag2 (so now FileName2.format would also match).  Essentially, instead of being:

if(baseTag[1] == tag[1] && baseTag[2] == tag[2] && baseTag[3] == tag[3]) {
          open(tag[0]);
     }

it would then be:

if(baseTag[1] == tag[1] && baseTag[3] == tag[3]) {
          open(tag[0]);
     }

if I decide to have it exclude the tag2 column.

The issue I'm having is making this change to the if statement be dynamic (without a huge mess of inefficient code).  Your mention of a switch statement sounds interesting.  I'm not the greatest programmer yet (pretty much just the really basic stuff) so I hadn't heard about this statement yet; but I'll certainly be looking it up now.  Anyway, hope the info helps clarify it a little better.