Solved

Iterating items in an array

Posted on 2003-10-21
10
172 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
[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
  • 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
Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

 
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

Do you have a plan for Continuity?

It's inevitable. People leave organizations creating a gap in your service. That's where Percona comes in.

See how Pepper.com relies on Percona to:
-Manage their database
-Guarantee data safety and protection
-Provide database expertise that is available for any situation

Question has a verified solution.

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

A web service (http://en.wikipedia.org/wiki/Web_service) is a software related technology that facilitates machine-to-machine interaction over a network. This article helps beginners in creating and consuming a web service using the ColdFusion Ma…
In our day to day coding, how many times have we come across a necessity to check whether a URL is a broken link or not? For those of you that answered countless and are using ColdFusion like myself, then this article is for you.  It will show yo…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

627 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