Solved

Iterating items in an array

Posted on 2003-10-21
10
170 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

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.

Question has a verified solution.

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

Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

763 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