looping though array and assigning values to dynamicaly named variables

i have a form which is generated by the user inputing 3-5 project names resulting in 150 to 250 checkboxes (50 per project) tthat form works perfectly but when the user submits this form i needt to generate a table that contains values calculated upont the users selections from the checkbox form, i am trying to use a doulble loop to count the number of selections based upon the value of the selections(there are 10 different values in the 50 questions per project)

i was trying to do this to loop through the projects and the fields

<cfloop from="1" to="arrayLen(arrProjects)" index="prj">
      <cfloop from="1" to="arrayLen(arrFields)" index="fld">
<cfswitch expression="right(arrFields[fld],2)">
      <cfcase value="P#prj#">
            <cfswitch expression="evaluate(arrFields[fld])">
                  <cfcase value="AA">
                        <cfif not isDefined('P#prj#AA')>
                              <cfset P#prj#AA = 1>   <<crashes on this

<cfswitch expression="right(arrFields[fld],2)"> i us this to get the project id of the answer field name is skill2Px where x=1-5

<cfswitch expression="evaluate(arrFields[fld])"> gets the value of the fiield (AA, EE, FF, QQ, WW,OO,ZZ etc) and based upon the value add 1 to a count

the final product is supposed to look like this

         P1      P2      P      P4       P5    TTL
AA     3        2        5      5         4     19
EE     4        1         1      0        0      6

any help would be appreciated
LVL 25
James RodgersWeb Applications DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

The line:   <cfset P#prj#AA = 1>
should be:   <cfset 'P#prj#AA' = 1>
James RodgersWeb Applications DeveloperAuthor Commented:
now i am getting this

CFCASE tag must have a constant value.

for this line

 <cfcase value="P#prj#">
you probably want: <cfcase value="#evaluate('P'&prj)#">
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

<cfcase value = Evaluate("P#prj#")>
James RodgersWeb Applications DeveloperAuthor Commented:
no neither suggestion worked, any ideas on a better way to acheive what i need?

i am really trying to turn a 5x50 2d arrayinto a 5x10 2d array by summig based upon the value...
any other methods you can suggest?
not really sure exactly what the goal is...
but I believe the first 3 lines should be:

<cfloop from="1" to="#LISTLEN(arrProjects)#" index="prj">
      <cfloop from="1" to="#LISTLEN(arrFields)#" index="fld">
            <cfif right(LISTGETAT(arrFields,fld),2) eq evaluate('P'&prj)>
that makes no sense.   how do you plan to add 50 rows together to make 10?
yeah confused !

what exactly are u looking for ?

the easiest way to count values is the listValueCount() function . Also, I would make P a structure. Look at this:

<!--- define a list of allpossible values --->
<cfset valueList = "AA,EE,FF,QQ,WW,OO,ZZ">
<!--- define the struct --->
<cfset P = StructNew()>
<!--- loop over project array --->
<cfloop from="1" to="arrayLen(arrProjects)" index="prj">
   <!--- make list from field array --->
   <cfset fieldList = ArrayToList(arrFields)>
   <!--- loop over possible values --->
   <cfloop list="#valueList#" index="v">
      <!--- count each value --->
      <cfset p[prj][v] = ListValueCount(fieldList, v)>

Now the result would be in e.g.
P.P1.AA, P.P2.AA, etc. You can also use array notation: P["P1"]["AA"]

If you do not want to have a struct, remove teh StructNew()-line and change the "count each value" line to
<cfset "P#prj##v#" = ListValueCount(fieldList, v)>



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
James RodgersWeb Applications DeveloperAuthor Commented:
>>how do you plan to add 50 rows together to make 10?
>>yeah confused !

the fifty rows only have ten possible values, 5 questions for each value,
the user reads each question and if it applied to the project then check it off when they have completed the form, they can answer one or all, i need to count the individual values by project and then sum the values by value, kind of a fake crosstab query, but from form input rather than a query
so that this



   Results Matrix    Project 1 Project 2 Project 3 Project 4 Project 5    total
AA                              2        0             3            5           1              11
WW                           3         1             2           2              2             10
EE                             2          3             3           3              3            14
FF                              0        0               2           4             4            10
ZZ                             1        1               1             1             1            5
QQ                            1         5               5             5            1            17
 i can parse out the project id from the field names and get the values but i am having a hrd time getting the counts
James RodgersWeb Applications DeveloperAuthor Commented:

this line is giving me an error

<cfset valueList = "AA,EE,FF,QQ,WW,OO,ZZ">

Invalid parser construct found on line 31 at position 18. ColdFusion was looking at the following text:

try using something else than valuelist it is a reserved word...make it value_list or listofvalues or anything ...valuelist is a function so CF is probably getting confused.
James RodgersWeb Applications DeveloperAuthor Commented:
thanks ...

realized that after a while

but then i get this error
 <cfset p[prj][v] = ListValueCount(fieldList, v)>
The member "1" in dimension 1 of object "p" cannot be found. Please, modify the member name.

which i tried to fix with this
<cfset p['P'&prj][v] = ListValueCount(fieldList, v)>
but got the same error

The member "P1" in dimension 1 of object "p" cannot be found. Please, modify the member name.

James RodgersWeb Applications DeveloperAuthor Commented:
changed procept's code to this

<cfset valuesList = "DD,GG,KK,NN,OO,RR,SS,TT,VV,ZZ">
<cfset P = structNew()>
<cfloop from="1" to="#arrayLen(arrProjects)#" index="prj">
   <cfset fieldList = ArrayToList(arrFields)>
   <cfloop list="#valuesList#" index="v">
        #PRJ# #V#
      <cfset structinsert(p ,'p'&#prj#&#v# ,ListValueCount(fieldList, v))>
        #P['p'&prj&v]# <br>

and i get this
1 DD 0
1 GG 0
1 KK 0
1 NN 0
1 OO 0
1 RR 0
1 SS 0
1 TT 0
1 VV 0
1 ZZ 0
2 DD 0
2 GG 0
2 KK 0
 it seems to be working but it insnt adding the count

sorry about the valueList error.. should have noticed it.

But, I also found that I misunderstood your setup, thus making my solution definitely wrong.

So arrFields just contains the fieldnames, right? Then actually you don't need it. ;-))

<!--- define the resulting struct --->
<cfset p = StructNew()>

<!--- loop over form --->
<cfloop collection="#form#" item="fld">

   <!--- extract project and field value --->
   <cfset proj = right(fld, 2)>
   <cfset keyVal = form[fld]>

   <!--- if "keyVal" is not already a key in "p", define the element --->
<cfif NOT StructKeyExists(p, keyVal)>
      <cfset p[keyVal] = StructNew()>
   <!--- if the project of the field is not yet a key in our struct, define it and set it to 1 --->
   <cfif NOT StructKeyExists(p[keyVal], proj)>
      <cfset p[keyVal][proj] = 1>

   <!--- else = it is defined: increment it --->
      <cfset p[keyVal][proj] = p[keyVal][proj] + 1>

Now you have a structure "p" that contains one key for each assigned value:
p.aa, p.ww or p["AA"], p["WW"], etc.

Each element has one element for each project where that value was assigned:
P.aa.p1, p.aa.p2 or p["AA"]["P1"], p["WW"]["P2"]

So if you want to have all the AA's of Project P5 it would be p.aa.p5 or p["AA"]["P5"]
if you want to sum up all AA's of all projects, just loop over p.aa:

<cfset total = 0>
<cfloop collection="#p['aa']#" item="key">
   <cfset total = total + p['aa'][key]>


James RodgersWeb Applications DeveloperAuthor Commented:
got it working with this...
a variation of Procept's code
<cfloop from="1" to="#arrayLen(arrProjects)#" index="prj">
<cfset valuesList ="P#prj#DD,P#prj#GG,P#prj#KK,P#prj#NN,P#prj#OO,P#prj#RR,P#prj#SS,P#prj#TT,P#prj#VV,P#prj#ZZ">
   <cfset fieldList = ArrayToList(arrFields)>
   <cfloop list="#valuesList#" index="v">
      <cfset "P#prj##v#" = ListValueCount(fieldList, v)>

thanks all
James RodgersWeb Applications DeveloperAuthor Commented:
i will try out your new code also, looks better than what i have
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Servers

From novice to tech pro — start learning today.