Skip navigation
Currently Being Moderated

Dynamic If Statment

Mar 21, 2012 12:11 PM

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[i].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


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!



  • Currently Being Moderated
    Mar 23, 2012 10:00 AM   in reply to dgolberg

    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.

    Mark as:
  • Currently Being Moderated
    Mar 23, 2012 2:01 PM   in reply to dgolberg

    oK, you can do this in one if statement but I think it is hard to read and understand.



    var tag1 = true;
    var tag2 = true;
    var tag3 = false;
    var baseTag = ['test.jpg','wood','dark','strong'];
    var tag = ['test.jpg','wood','dark','light'];
    if((!tag1 || baseTag[1] == tag[1]) && (!tag2 || baseTag[2] == tag[2] ) && ( !tag3 || baseTag[3] == tag[3] ) /*etc*/) {
    alert('load file');


    You have have if statement that uses the AND operator. In order for that to test true all the expressions need to test true. So for each expression you create another set of expression that uses the OR operator. With the OR operator if either are true it test true.


    So if you want baseTag[1] == tag[1] to test true if tag1 is false( skip this tag ) you create an expression that will test true if tag1 == false. !tag1 || baseTag[1] == tag[1] You then wrap with () so it makes one expression as part of your AND comparisons

    Mark as:
  • Currently Being Moderated
    Mar 23, 2012 3:11 PM   in reply to dgolberg

    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');
    var csvFile = File.openDialog("Open Comma-delimited File","comma-delimited(*.csv):*.csv;");'r');
    var Data =;
    Data = Data.split('\n');
    Data.shift(); //remove header line
    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,'');
    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
    /**************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
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points