Skip to main content
Inspiring
Jul 2, 2019 7:08 PM UTC
Question

ArrayIsDefined and numeric

Hi,

Is it possible to check if an Array is defined and in the same CFIF statement check to make sure it's numeric?

Something like:

<cfif ArrayIsDefined(F,2) AND IsNumeric>

Taking it one step further I need to make sure the value is between 1 and 99.

ArrayIsDefined and IsNumeric and GT 0 and LT 100....

Suggestions?

Thanks in advance.

Gary

This topic is closed to new replies. Start a new post to keep the conversation going.

17 replies

ghanna1Author
Inspiring
Jul 3, 2019 11:46 AM UTC

Every time I post something simple here I walk away learning so many new things! I'm four months into cleaning up craptastic code written in 2008 by a former employee and each pass over the code I'm finding more efficient and more secure ways of doing things. 

Translate
WolfShade
Legend
Jul 3, 2019 2:47 PM UTC

There are a lot of differences between code today and decade-old code.    CF has come a long way, for sure.  The ESAPI, alone, is a big difference.

V/r,

^ _ ^

Translate
BKBK
Community Expert
Community Expert
Jul 3, 2019 11:09 AM UTC

ghanna1  wrote

Is it possible to check if an Array is defined and in the same CFIF statement check...

That calls for something like

isdefined("myArray"), isdefined("variables.myArray") or structKeyExists(variables,"myArray")

Translate
BKBK
Community Expert
Community Expert
Jul 2, 2019 9:30 PM UTC

Noticing the part about 1 and 99, I updated my post.

Translate
WolfShade
Legend
Jul 2, 2019 8:15 PM UTC

<cfset allAreNumeric_1to99 = true />

<cfswitch expression="#arrayIsDefined(myArray,1)#"><!--- checks if there is at least one item in array --->

     <cfcase value="yes">

          <cfloop from="1" to="#ArrayLen(myArray)#" index="idx">

               <cfif val(myArray[idx]) gte 1 AND val(myArray[idx]) lte 99>

                    <!--- within parameters, do nothing --->

               <cfelse>

                    <cfset allAreNumeric_1to99 = false />

                    <cfbreak />

               </cfif>

          </cfloop>

     </cfcase>

     <cfdefaultcase>It's not an array!  EPIC FAIL!</cfdefaultcase>

</cfswitch>

<cfoutput>#allAreNumeric_1to99#</cfoutput>

HTH,

^ _ ^

Translate
ghanna1Author
Inspiring
Jul 2, 2019 8:48 PM UTC

Wolfshade... That made my novice brain hurt. Now I have to create a test page to play with that code and teach myself what the heck it does. Thanks!!!!

Translate
WolfShade
Legend
Jul 2, 2019 8:59 PM UTC

It's very simple, actually... it's just that the switch/case might make it appear difficult.  But I prefer switch/case over if/else.

First, I'm setting a variable called allIsNumeric_1to99 to true.

Next, I'm using a switch/case to check if the array is defined; this will also check to make sure that there is at least one item in the array.

If it's an array, then loop through it (iterate it) checking that the value of the current position is between 1 and 99.  If it is less than one or higher than 99, it sets that first variable to false and breaks out of the loop.  (HINT:  Using val() on a string like "a" will produce a value of zero.)

If it's NOT an array (cfdefaultcase), then it displays "It's not an array! EPIC FAIL!".

After the switch/case, it outputs the value of #allAreNumeric_1to99#.  Either true or false.

HTH,

^ _ ^

Translate
pete_freitag
Participating Frequently
Jul 2, 2019 7:57 PM UTC

Hi Gary,

Sure, you can just use the AND operator to add multiple conditions to the cfif statement:

<cfif ArrayIsDefined(F,2) AND isNumeric(f[2]) AND f[2] GTE 1 AND f[2] LTE 99>

The conditions are evaluated using short circuit logic, so if ArrayIsDefined(F,2) is false, then isNumeric(f[2]) is never executed.

Pete Freitag

Foundeo Inc.

Translate
ghanna1Author
Inspiring
Jul 2, 2019 8:45 PM UTC

Pete,  Thanks... I was struggling with the [ ]. Stupid mistake - I was doing #F[2]#

Translate
BKBK
Community Expert
Community Expert
Jul 2, 2019 7:55 PM UTC

<!---  'gte' assumes that the value of myArray[2] may include 1 or 99. If not, use 'gt' instead --->

<cfif isdefined("variables.myArray") and arrayIsDefined(myArray, 2) and isNumeric(myArray[2]) and (myArray[2]-1)*(99-myArray[2]) gte 0>

</cfif>

or

<cfif structKeyExists(variables, "myArray") and arrayIsDefined(myArray, 2) and isNumeric(myArray[2]) and (myArray[2]-1)*(99-myArray[2]) gte 0>

</cfif>

Translate