Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array Question 4

Status
Not open for further replies.

alsjka

Technical User
Jan 21, 2003
114
US
I have the follow code I am trying to run with no luck. I am not that familar with arrays so If someone could shed some light on this it would be great.

Here is what I am trying to do. I want to get live information from a machine store the value in a array(?) then refresh the page 1 min later and add the new live value to the array. This will continue for 8 hours then the array will be cleared. Is this possible. ARGH!



<cfset session.myarray=ArrayNew(1)>
<cfset session.myarray2=ArrayNew(1)>
<cfset session.myarray3=ArrayNew(1)>

<cfquery name="one" datasource="source">
select top 5 tagname, value,datetime
from table
where tagname = total_produced'
</cfquery>


<CFSET arrayappend(session.myarray,one.value)>

<cfoutput>
#session.myarray[1]#<br>
</cfoutput>

 
Try this link, it goes through arrays step by step and has some pretty good examples.


But, to give you a simple example:
Code:
<CFQUERY NAME="Customer" DATASOURCE="#DSN#">
SELECT	CustID, CustName
FROM	dbo.Customers
</CFQUERY>

<cfset CustomerNames=arraynew(1)>

<cfloop query="Customer">
  <cfset CustomerNames[CustID]=CustName>
</cfloop>
<!--- Displays the Customer array --->
<cfoutput>
 <cfloop query="Customer">
   #CustomerNames[CustID]#<br>
 </cfloop>
</cfoutput>



Hope This Helps!

Ecobb
Beer Consumption Analyst

"My work is a game, a very serious game." - M.C. Escher
 
It helps yes and no. I still need to find out if it possible to append a seeion.array when a page is refreshed.

I have a page that runs from say 8:00am that has a query that gives me a value (1000) then 5 min later the value will be say (2000) so i should have something like this (1000, 2000 and so on) if i did a arraytolist. I know how to loop through a query to append an array and store session variables.

How do I append to my original session.array when the page is refreshed.

Hope I explained it better.

 
I do session arrays.. lots of people do...

And good job ecobb.

Code:
[red]<cfif not isDefined("CustomerNames")>[/red]
  <cfset CustomerNames=arraynew(1)>
[red]</cfif>[/red]
<cfloop query="Customer">
  <cfset CustomerNames[CustID]=CustName>
</cfloop>
<!--- Displays the Customer array --->
<cfoutput>
 <cfloop query="Customer">
   #CustomerNames[CustID]#<br>
 </cfloop>
</cfoutput>

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Forgot a crucial part... making the array a session value...

Code:
<cfif not isDefined("CustomerNames")>
  <cfset CustomerNames=arraynew(1)>
</cfif>
<cfloop query="Customer">
  <cfset CustomerNames[CustID]=CustName>
</cfloop>
<!--- Displays the Customer array --->
<cfoutput>
 <cfloop query="Customer">
   #CustomerNames[CustID]#<br>
 </cfloop>
</cfoutput>
[red]<Cflock timeout="10" scope="session">
  <Cfset Session.MyArray=CustomerNames>
</cflock>[/red]

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Hey webmigit, that's pretty neat. I never thought about putting the entire array into a session like that. I do have one question for you, though. On the IsDefined, how would I check for the existance of a particular value in the array? Something like IsDefined("CustomerNames[4]") (only that doesn't work).



Hope This Helps!

Ecobb
Beer Consumption Analyst

"My work is a game, a very serious game." - M.C. Escher
 
I never did find a reasonable solution to that..

When I need t odo that, I append the elements to a list and carry it with the array.. there seems to no ArrayElementDefined() or anything like it.

The other method is a try/catch on it.. and that's not very good either.

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Thanks to you both Ecobb & Webmigit all the information is now tied together. Just one note i see you both are looping through your query I do not need to loop through the query to grab the data just run it one time every 5 min and place the value in the array. My live data table has one value at any given time and I am running 7 queries to grab all the data needed will your options still work?


Thanks Again..
 
Yeah they'll still work... the looping bit was because you shared psuedo-code so we assumed...

Just reference #queryname.field#.

If you want, post the queries... 7 queries... maybe that can be shortened...

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Here are my 7 queries I want to run every 5 min. I tried running without the loops and see below. I feel really stupid now because i am confused again.



<cfquery name="get_24pack1" datasource="BSIMFG_runtime">
select value, tagname,datetime
from v_analoglive
where tagname = 'c24pack1_dat_prod_Count'
</cfquery>

<cfquery name="get_24pack2" datasource="BSIMFG_runtime">
select value, tagname,datetime
from v_analoglive
where tagname = 'c24pack2_dat_prod_Count'
</cfquery>

<cfquery name="get_24pack3" datasource="BSIMFG_runtime">
select value, tagname,datetime
from v_analoglive
where tagname = 'c24pack3_dat_prod_Count'
</cfquery>

<cfquery name="get_24pack4" datasource="BSIMFG_runtime">
select value, tagname,datetime
from v_analoglive
where tagname = 'c24pack4_dat_prod_Count'
</cfquery>

<cfquery name="get_24pack5" datasource="BSIMFG_runtime">
select value, tagname,datetime
from v_analoglive
where tagname = 'c24pack5_dat_prod_Count'
</cfquery>

<cfquery name="get_24pack6" datasource="BSIMFG_runtime">
select value, tagname,datetime
from v_analoglive
where tagname = 'c24pack6_dat_prod_Count'
</cfquery>

<cfquery name="get_24pack7" datasource="BSIMFG_runtime">
select value, tagname,datetime
from v_analoglive
where tagname = 'c24pack7_dat_prod_Count'
</cfquery>


<cfset CustomerNames=arraynew(1)>

<cfset CustomerNames[CustID]=CustName>

<Cflock timeout="10" scope="session">
<Cfset Session.MyArray=CustomerNames>
</cflock>

<cfoutput>

#session.myarray#

</cfoutput>

The way it is now produces an error(Variable CUSTID is undefined). If I use an arrayappend each time the page runs my array just gets replaced with the new value not appended.

 
The array examples we gave you were just that, an example. They were not prewritten to match your design, so you can't just copy and paste them and expect them to work. Look at the code I gave you, it was pulling from a fake customers table. Unless you have that table with a CustID and CustName fields in it, the code I gave you won't work unless you change the names of the query values to match your own.

That's why you get "Variable CUSTID is undefined"...



Hope This Helps!

Ecobb
Beer Consumption Analyst

"My work is a game, a very serious game." - M.C. Escher
 
Sorry I copied the wrong code I was testing. this is what I was testing below.


<cfquery name="get_24pack1" datasource="BSIMFG_runtime">
select tagname as custname, value as custid
from v_analoglive
where tagname = 'c24pack1_dat_prod_Count'
</cfquery>

<cfquery name="get_24pack2" datasource="BSIMFG_runtime">
select tagname as custname, value as custid
from v_analoglive
where tagname = 'c24pack2_dat_prod_Count'
</cfquery>

<cfquery name="get_24pack3" datasource="BSIMFG_runtime">
select tagname as custname, value as custid
from v_analoglive
where tagname = 'c24pack3_dat_prod_Count'
</cfquery>

<cfquery name="get_24pack4" datasource="BSIMFG_runtime">
select tagname as custname, value as custid
from v_analoglive
where tagname = 'c24pack4_dat_prod_Count'
</cfquery>

<cfquery name="get_24pack5" datasource="BSIMFG_runtime">
select tagname as custname, value as custid
from v_analoglive
where tagname = 'c24pack5_dat_prod_Count'
</cfquery>

<cfquery name="get_24pack6" datasource="BSIMFG_runtime">
select tagname as custname, value as custid
from v_analoglive
where tagname = 'c24pack6_dat_prod_Count'
</cfquery>

<cfquery name="get_24pack7" datasource="BSIMFG_runtime">
select tagname as custname, value as custid
from v_analoglive
where tagname = 'c24pack7_dat_prod_Count'
</cfquery>


<cfset CustomerNames=arraynew(1)>

<cfset CustomerNames[CustID]=CustName>

<Cflock timeout="10" scope="session">
<Cfset Session.MyArray=CustomerNames>
</cflock>

<cfoutput>

#session.myarray#

</cfoutput>


 
I tried to find an answer to your "ArrayElementDefined()"

livedocs said:
The IsDefined function always Returns False if you specify an array or structure element using bracket notation. For example IsDefined("myArray[3]") always returns False, even if the array element myArray[3] has a value. To check for the existence of an array element, copy the element to a simple variable and use IsDefined to test whether the simple variable exists.

can someone test this before i yell at MM? Best i can tell this is a big fat hairy lie.

I tried it with the following code and got an error. (notice the element i used does exist and i still get the error)
Code:
<cfset myArray = arrayNew(1)>
<cfset myArray[1] = "bob">
<cfset myArray[3] = "joe">
<cfif isdefined("myArray[1]")> 
	yes
<cfelse>
	no
</cfif>
error said:
Parameter 1 of function IsDefined, which is now "myArray[1]", must be a syntactically valid variable name.

secondly, how can you
livedocs said:
copy the element to a simple variable and use IsDefined to test whether the simple variable exists.
if using the element produces this error
error2 said:
The element at position 2 in dimension 1 of object "myArray" cannot be found. The object has elements in positions 1 through 3. Please, modify the index expression.


A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.
-Douglas Adams (1952-2001)
 
Thank You! That's why I originally asked the question! I thought that you should be able to do it, and every way I could think of to try I kept erroring out. I was "gracefully" trying to see if any of you guys had figured it out. ;-)

I'm glad I'm not the only one, I don't feel so stupid now.



Hope This Helps!

Ecobb
Beer Consumption Analyst

"My work is a game, a very serious game." - M.C. Escher
 
even though webmigit said it isn't such a great idea...
i used cfscript to make a udf to check for the element. it uses try and catch to do it.

Code:
<cfscript>
	function isArrayElementDefined(arrayName){
		try{
			testVar = evaluate(arrayName);
			return true;
		}catch(any test){
			return false;
		}
	}
</cfscript>
<cfset myArray = arrayNew(1)>
<cfset myArray[1] = "bob">
<cfset myArray[3] = "joe">
<cfoutput>
<cfloop from = "1" to = "3" index = "place">
#isArrayElementDefined("myArray[#place#]")#
</cfloop>
</cfoutput>
output is
true false true

what do you guys think? FAQ worthy?


A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.
-Douglas Adams (1952-2001)
 
Ooo...Very Nice! Definately needs to be an FAQ. Have you blasted MM about it yet?



Hope This Helps!

Ecobb
Beer Consumption Analyst

"My work is a game, a very serious game." - M.C. Escher
 
not yet, on my way..

A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.
-Douglas Adams (1952-2001)
 
Yeah but webmigit said that because webmigit thinks that MM should have taken care of this.. :)

Your method is nice, but you must agree, its a bit overkill when there SHOULD be a method..

The excerpts you posted were funny... great.. copy the element to a variable... trouble is, you can't copy an element that doesn't exist, so you still need to know how to do it.

Can I make a revision that should make the udf run faster?

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Oh, the funny part is that they have the code to do it... I mean, the code that processes arrays returns an error if the array doesn't exist.. but in order to return that, they're doing an existence test.

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
revise away you know i'm all for it!

A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.
-Douglas Adams (1952-2001)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top