looping over a structure using cfscript

Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.

  1. #1

    Default looping over a structure using cfscript

    Hi,

    I am passing form variables to a function. What i want is to be abled to loop
    throught the form variables which is a structure and then for each value in the
    structure do a trim.

    I have something like this but i don't think it is right

    for (i=1; i lte structcount(arguments.form); i=i+1 ) {
    //writeoutput(arguments. & "<Br>");
    // need to trim the current value
    }

    Cany anyone please help or point me in the right direction?
    Thanks

    zubair Guest

  2. Similar Questions and Discussions

    1. Class::Struct - want to access structure within structure
      I want to access a structure within a structure. Below is what I had in mind. Please help. #!/perl/bin/perl use Class::Struct; struct Step...
    2. Looping through a structure with duplicate key names
      It may be that I'm barking up the wrong tree here, and there's a nice function that will do this, but I want to turn an XML document returned to me...
    3. cfscript
      since i cannot find the cf and java integration thread, gues i will post this here: i wrote a cfm with cfscript that utilizes java which connects...
    4. <cfscript> autocaps problem </cfscript> - betcha can't fix this!
      First, I'll tell you up front that I am clueless when it comes to cfscript. So I apologize if this question is off topic. I have a nice little...
    5. cfscript and variables
      I'm using cfscript against a COM object. In VB the paramater is passed like: Set oSegment = oTransactionset.CreateDataSegment('BEG')...
  3. #2

    Default Re: looping over a structure using cfscript

    you need a for in loop. be careful as you're going to get ALL of the form scope
    (fieldnames, submit button, etc.) when you do this.


    for (i IN arguments.form) {
    writeoutput("#arguments.form[i]#<br>");
    }

    PaulH Guest

  4. #3

    Default Re: looping over a structure using cfscript

    Hi thanks for the reply. Is there also a way of deleting the fieldnames, submit
    button etc attributes from the form struct as you mentioned? Also is it a case
    of just using

    trim(arguments.form)

    to automatically trim all from variables?

    Thanks

    zubair Guest

  5. #4

    Default Re: looping over a structure using cfscript

    you'd probably want to either copy the form scope to another structure (if
    you're going to delete these using StructDelete function) or ignore them.
    beyond the standard key for FIELDNAMES, everything else depends on your form.

    no, you'll have to trim each value in the structure.

    for (i IN arguments.form) {
    if (i NEQ "FIELDNAMES")
    writeoutput("#trim(arguments.form[i])#<br>");
    }

    PaulH Guest

  6. #5

    Default Re: looping over a structure using cfscript

    Thanks,

    i'm doing this. Do you think this is a good way of doing it?

    Thanks for your input


    <!--- function to get the event and outcome dates --->
    <cffunction name="trimVariables" access="public" output="Yes"
    returntype="struct" hint="I trim whitespace of variables">
    <cfargument name="form" required="Yes" type="struct">

    <cfscript>

    if ( StructIsEmpty(arguments.form) is 'NO' ) {
    //writeoutput("form structure is not empty");

    excludedFormElements = 'FIELDNAMES';


    // loop through the form struct
    for (i IN arguments.form) {
    // make sure current key it structure being looped exists
    if ( StructKeyExists(arguments.form, i) ) {
    writeoutput("<br>#i# - #arguments.form#<br>");
    // delete from the struct items in the
    excludedFormElements list
    if ( ListContains(excludedFormElements, i, ',') ) {
    writeoutput("exclude this key");
    temp = structdelete(arguments.form, i);

    } else {
    writeoutput("fine");
    // trim variable
    //arguments.form = "hi";
    arguments.form = trim(arguments.form);
    //if ( arguments.form eq 1 ) {
    //arguments.form = "zzzzzzzz";
    //}
    }
    }

    }


    }
    </cfscript>

    <cfreturn arguments.form>

    </cffunction>

    zubair Guest

  7. #6

    Default looping over a structure using cfscript

    You could delete the fieldnames key at the beginning of your code eliminating the re-occuring if statement.

    <cfscript>
    if ( structKeyExists(form,"fieldnames") structDelete(form,"fieldnames");
    </cfscript>
    CJM Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139