Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Iterating items in an array

Posted on 2003-10-21
10
Medium Priority
?
173 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
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

This is a guide to setting up a new WHM/cPanel Server to be used for web hosting accounts. It is intended for web hosting company administrators and dedicated server owners. For under $99 per month (considering normal rate of Big Data Cetnters like …
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

722 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