Skip to main content
Inspiring
July 2, 2019
Question

ArrayIsDefined and numeric

  • July 2, 2019
  • 6 replies
  • 1315 views

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 has been closed for replies.

6 replies

ghanna1Author
Inspiring
July 3, 2019

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. 

WolfShade
Legend
July 3, 2019

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,

^ _ ^

BKBK
Community Expert
Community Expert
July 3, 2019

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")

BKBK
Community Expert
Community Expert
July 2, 2019

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

WolfShade
Legend
July 2, 2019

<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,

^ _ ^

ghanna1Author
Inspiring
July 2, 2019

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!!!!

WolfShade
Legend
July 2, 2019

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,

^ _ ^

pete_freitag
Participating Frequently
July 2, 2019

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.

ghanna1Author
Inspiring
July 2, 2019

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

BKBK
Community Expert
Community Expert
July 2, 2019

<!---  '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>