Solved

looping though array and assigning values to dynamicaly named variables

Posted on 2003-11-13
17
2,253 Views
Last Modified: 2013-12-24
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
0
Comment
Question by:James Rodgers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 3
  • 2
  • +4
17 Comments
 
LVL 15

Assisted Solution

by:danrosenthal
danrosenthal earned 50 total points
ID: 9743351
The line:   <cfset P#prj#AA = 1>
should be:   <cfset 'P#prj#AA' = 1>
0
 
LVL 25

Author Comment

by:James Rodgers
ID: 9743419
now i am getting this

CFCASE tag must have a constant value.


for this line

 <cfcase value="P#prj#">
0
 
LVL 15

Expert Comment

by:danrosenthal
ID: 9743456
you probably want: <cfcase value="#evaluate('P'&prj)#">
0
Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

 
LVL 2

Expert Comment

by:Crazee
ID: 9743462
<cfcase value = Evaluate("P#prj#")>
0
 
LVL 25

Author Comment

by:James Rodgers
ID: 9743544
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?
0
 
LVL 15

Expert Comment

by:danrosenthal
ID: 9743959
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)>
0
 
LVL 10

Expert Comment

by:substand
ID: 9744446
that makes no sense.   how do you plan to add 50 rows together to make 10?
     
0
 
LVL 17

Expert Comment

by:anandkp
ID: 9745253
yeah confused !

what exactly are u looking for ?
0
 
LVL 4

Accepted Solution

by:
procept earned 450 total points
ID: 9746000
Hi,

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)>
   </cfloop>
</cfloop>

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)>

HTH,

Chris

0
 
LVL 25

Author Comment

by:James Rodgers
ID: 9747635
>>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

Form.SKILL1P1 AA
Form.SKILL1P2 EE
Form.SKILL1P3 FF
Form.SKILL1P4 AA
Form.SKILL1P5 EE
Form.SKILL2P1 AA
Form.SKILL3P2 EE
Form.SKILL4P3 EE
Form.SKILL5P4 EE
Form.SKILL6P5 ZZ
Form.SKILL7P4 ZZ
Form.SKILL8P3 ZZ
Form.SKILL9P2 ZZ
Form.SKILL10P1 FF
Form.SKILL11P2 WW
Form.SKILL12P3 AA
Form.SKILL13P4 WW
Form.SKILL14P5 FF

becomes

   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
OO
 
 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
0
 
LVL 25

Author Comment

by:James Rodgers
ID: 9747861
procept,

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:

=
0
 
LVL 6

Expert Comment

by:PE_CF_DEV
ID: 9748201
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.
0
 
LVL 25

Author Comment

by:James Rodgers
ID: 9748724
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.

0
 
LVL 25

Author Comment

by:James Rodgers
ID: 9748992
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>
   </cfloop>
</cfloop>

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
etc.
 it seems to be working but it insnt adding the count
0
 
LVL 4

Assisted Solution

by:procept
procept earned 450 total points
ID: 9749234
Hi,

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()>
   </cfif>
   
   <!--- 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 --->
   <cfelse>
      <cfset p[keyVal][proj] = p[keyVal][proj] + 1>
   </cfif>
</cfloop>

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]>
</cfloop>

HTH,

Chris
0
 
LVL 25

Author Comment

by:James Rodgers
ID: 9749370
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)>
   </cfloop>
</cfloop>

thanks all
0
 
LVL 25

Author Comment

by:James Rodgers
ID: 9749391
i will try out your new code also, looks better than what i have
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Server specifications for web hosting 7 96
Using random iterations in password hashing.  Good or Bad? 4 99
ip / url redirect 13 74
Link failure 16 91
One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question