Solved

Iterating items in an array

Posted on 2003-10-21
10
169 Views
Last Modified: 2013-12-24
Based on this functionality below I have 2 questions:

!--- create an array of days and buckets starting with the first_date going
until the last_date--->

<cfset col = "293">
<cfset myArrayOfDays = ArrayNew(1)>
<cfset myArrayOfDays[#first_date#] = structNew()>
<cfset myArrayOfDays[#first_date#].bucket = #col#>
<cfset col=#col#+30>
<cfloop index="counter" from="2" to="999" step="1">
        <cfset temp_date = #DateAdd("d",counter-1,#first_date#)#>
        <cfset temp_date_formatted =
#DateFormat("#temp_date#","mm/dd/yyyy")#>
        <cfset myArrayOfDays[#temp_date_formatted #] = structNew()>
        <cfset myArrayOfDays[#temp_date_formatted #].bucket = #col#>
        <cfset col=#col#+30>
        <!---<cfif temp_date eq last_date>--->
        <!--- -1 less than, 0 equal, 1 greater than --->
        <cfif DateCompare(#last_date#, #temp_date#, "d") eq 0 >
                <cfbreak>
        </cfif>
</cfloop>

<!--- create an array adding the buckets, matching on Days--->
<cfset RectDays = arrayNew(1)>
<cfoutput query="GetTask" startrow="#StartRow#" maxrows="#MaxRows#">
        <cfset RectDays[#GetTask.CurrentRow#] = structNew()>
        <cfset RectDays[#GetTask.CurrentRow#].TaskID = #GetTask.TASK_ID#>
        <cfset RectDays[#GetTask.CurrentRow#].TaskDUR = #GetTask.TASK_DUR#>
        <cfset RectDays[#GetTask.CurrentRow#].PerComp = #TASK_PER_COMP#>


        <!---  for each task using the date, grab the bucket--->
        <cfset ck = #DateFormat("#GetTask.TASK_BASE_START#","mm/dd/yyyy")#>

        <!---  find the starting day for the baseline start --->
        <cfset found = false>

1) is there another way that I can look right into the index to find this
instead of looping like this?

        <cfloop from="1" to="#myArrayOfDays_cnt#" index="i">
                <cfset myvar = "#myArrayOfDays[i]#">
          <!---  should be in the format: #DateFormat("#temp_date#","mm/dd/yyyy")# --->

         <cfif #ck# eq #myvar#>
       <cfset RectDays[#GetTask.CurrentRow#].BasePosition = #myArrayOfDays[i].bucket#>
                        <cfset found = true>
                        <cfbreak>
                </cfif>
        </cfloop>

        <cfif found eq false>
                <cfset RectDays[#GetTask.CurrentRow#].BasePosition = "999">
        </cfif>
</cfoutput>

2) If I wanted to iterate through the myArrayOfDays array what is the
correct way to do this?

<cfoutput>
        <cfloop from="1" to="#ArrayLen(myArrayOfDays)#" index="i">
                #myArrayOfDays[i].date#  #myArrayOfDays[i].bucket#<br>
        </cfloop>
</cfoutput>

You can see this is not correct because the index is in days like 12/30/2003
and not 1,2,3,4,5 etc.

Mike
0
Comment
Question by:hillmw
  • 4
  • 2
  • 2
  • +1
10 Comments
 
LVL 9

Expert Comment

by:CFDevHead
ID: 9592015
what is the value of  #first_date#
0
 
LVL 2

Expert Comment

by:jonnygo55
ID: 9592167
perhaps you shouldnt use a date as your array index and instead use any particular date as a number of days away from a set date....

i.e.
<cfset someDate='1/1/1900'>
<cfset numDaysSomeDatetoNow = DateDiff('d',now(),someDate)>

use the that as the array index...

<cfset myArrayOfDays[#numDaystoNow#] = structNew()>...
...
0
 

Author Comment

by:hillmw
ID: 9592382
The "#myArrayOfDays# is going to be:

index  - bucket
12/30/2002 - 200
12/31/2002 - 225
1/1/2003 - 250
.
.
.
11/20/2003 - (bucket)

until #last_date# is reached
0
Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

 
LVL 9

Expert Comment

by:CFDevHead
ID: 9592608
So what is first_date?
0
 

Author Comment

by:hillmw
ID: 9592644
firstdate varies, but in the example I provided above:
#first_date# = "12/30/2002"
#last_date# = "1/1/2003"

The array then encompasses every day from 12/30/2002 to 1/1/2003 with a corresponding element I call a bucket.
0
 

Author Comment

by:hillmw
ID: 9593357
I am answering question 1) myself. Here is the answer:

I should use:

<cfset ck = #DateFormat("#GetTask.TASK_BASE_START#","mm/dd/yyyy")#>
<cfif NOT StructISEmpty(myArrayOfDays['#ck#'])>
  <cfset RectDays[#GetTask.CurrentRow#].BasePosition = #myArrayOfDays['#ck#'].bucket#>
  <cfelse>
    <cfset RectDays[#GetTask.CurrentRow#].BasePosition = "0">            
</cfif>

Question 2) is still open however
0
 
LVL 2

Expert Comment

by:jonnygo55
ID: 9593438
how about?

<cfoutput>
        <cfloop from="1" to="#ArrayLen(myArrayOfDays)#" index="i">
                <cfset thisDateIndex = DateAdd('d',i-1,first_date)>
                #myArrayOfDays[thisDateIndex].date#  #myArrayOfDays[thisDateIndex].bucket#<br>
        </cfloop>
</cfoutput>
0
 

Author Comment

by:hillmw
ID: 9610449
I still have this question open the guts of which were really answered in another post on looping so here is a related array question.

myarrayx looks like:
index id     comp
1       3      100
2       6      50
3       9      25

myarrayy looks like:
index id     relid
1       3     9
2       3     10  
3       14   11

so you can see that in myarrayx the id is unique, but in myarrayy id it is one-to-many.

I don't want to have to loop through every item in myarrayy where the id matchs in myarrayx.

I currently have this logic:

<cfloop from="1" to="#ArrayLen(myarrayx)#" index="i">
      <cfset test = #myarrayx[i].id#>
      <cfloop from="1" to="#ArrayLen(myarrayy)#" index="j">
            <cfif #myarray[j]id# eq #test#>                        
                       <td>#myArrayOfRels[j].relid#</td>
            </cfif>
      </cfloop>                  
</cfloop>

I first created a 2 dim structure for myarrayy using the id as the index and a counter as the second dimension but I had trouble because myarrayy[1][1] and myarrayy[2][1] did not exist.

myarrayy[3][1].relid contained 9
myarrayy[3][2].relid contained 10

I wanted to use:

<cfif not structisempty(myarrayy[#i#][1])>
   ..... get the relid .. do some processing
</cfif>

but I kept on getting an error that it was empty ... duh! but when i was equal to 3 I got some data.

Any help is appreciated. I'd like to have an array format where I don't have to loop through each item in the array to get the data.

Mike
0
 

Accepted Solution

by:
PashaMod earned 0 total points
ID: 11006596
PAQed, with points refunded (250)

PashaMod
Community Support Moderator
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
OpenLDAP set password to expire 7 696
LAMP problem identifier tool ? 9 122
New OSQA server has a ton of fake users 4 71
Company website 6 30
Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

839 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