|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Insert Structure into 2-dimensional Array?
Introduction - I'm just learning arrays and structures in CF MX. I am making a schedule with Mon-Sun up top and 9am-2am the following day going down the left-most column.
I have already accomplished this using one very lengthy method, but I'm trying to combine it all into one big cfloop. What I want to know as of now... I have a 2-dimensional Array where we have say TimeArray[timeofday][episode]. I would like to make "episode" as a structure consisting of two keys - one for the episode name, and a second signifying a rowspan number (i.e. if episode = 30 minutes then rowspan = 1. if episode = 1 hour then rowspan = 2, etc.). So in the end, I can reference as something like: TimeArray[9:00am][Episode.Name AND Episode.Rowspan] Is this even possible? I have most of the logic down, but I can't seem to figure out how to attach this structure for each element in the second portion of the array. I keep getting things like "Object of type class java.lang.String cannot be used as an array" etc. The basics of the code I have so far is attached below. This is of course NOT LITERAL, but summarized. It is basically searching through a database for a specific time, then adding it to the array when found: Code:
<cfset Time_Start = CreateTime(09,00,00)>
<cfset TimeArray = ArrayNew(2)>
<cfloop index="time" from="1" to="36">
<cfset TimeArray[time] = Time_Start>
<cfloop index="SearchAllEpisodes" from="1" to="#scheduledata.end#">
<cfif scheduledatabase.Time = Time_Start>
<cfset CurrentEpisode = scheduledata.Episode[SearchAllEpisodes]>
<cfset Episodes = structNew()>
<cfset Episodes.Name = CurrentEpisode>
<cfif scheduledata.Duration[SearchAllEpisodes] = 30 minutes>
<cfset Episodes.Rowspan = "1">
<cfelseif scheduledata.Duration[SearchAllEpisodes] = 1 hour>
<cfset Episodes.Rowspan = "2">
</cfif>
<CODE HERE to somehow append the structure Episodes to each element in second dimension of TimeArray[time]>
</cfif>
</cfloop>
<cfset Time_Start = Time_Start + 30 minutes>
</cfloop>
|
|
#2
|
|||
|
|||
|
Quote:
Not like that, first of all an array is indexed numerically, there is no way you have an array that is indexed like timeArray[9:00am]. It can only be something like timeArray[1]. But you can make episode a structure and do it like this: #timeArray[1].episode.name# #timeArray[1].episode.rowSpan# You'd do that like this: <cfscript> timeArray = arrayNew(1); timeArray[1].episode = structNew(); timeArray[1].episode.name = "the name"; timeArray[1].episode.rowSpan = 5; // and if you wanted to have the time as a structure element... timeArray[1].episode.time = "9:00 am"; </cfscript>
__________________
Ask if you have a question, but also help answer questions that you have knowledge of! Thanks, Brian. How to Post a Question in the Forums |
|
#3
|
|||
|
|||
|
Quote:
Thanks - I'll try this out. The 9:00am reference was just an example piece of data. I wasn't being literal. |
|
#4
|
|||
|
|||
|
I keep getting the following error. Could someone explain this to me in simple terms?
"You have attempted to dereference a scalar variable of type class java.lang.String as a structure with members." For instance - why doesn't something as simple as the following work?! Here I get "The element at position 1 of dimension 1, of array variable "TIMEARRAY," cannot be found. " Code:
<cfset timeArray = ArrayNew(1)>
<cfloop index="i" from="1" to="36">
<cfscript>
timeArray[i].Episodes = structNew();
timeArray[i].Episodes.Name = "CurrentEpisode";
</cfscript>
</cfloop>
There's some very basic syntax that I'm missing here and there, and every once in a while it drives me nuts! ![]() |
|
#5
|
|||
|
|||
|
Quote:
Confused kiteless...have you tried this code yourself? It comes back with said error above: "The element at position 1 of dimension 1, of array variable "TIMEARRAY," cannot be found." |
|
#6
|
|||
|
|||
|
No I wrote it off the top of my head. This works:
<cfscript> timeArray = arrayNew(1); timeArray[1] = structNew(); timeArray[1].episode = structNew(); timeArray[1].episode.name = "the name"; timeArray[1].episode.rowSpan = 5; // and if you wanted to have the time as a structure element... timeArray[1].episode.time = "9:00 am"; </cfscript> |
|
#7
|
|||
|
|||
|
This makes sense now - thank you!
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > ColdFusion Development > Insert Structure into 2-dimensional Array? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|