Solved

Iterating items in an array

Posted on 2003-10-21
10
166 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
 
LVL 9

Expert Comment

by:CFDevHead
ID: 9592608
So what is first_date?
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now