isDefined; structure with a numeric key

Hi!

I tried to create a structure with a numeric key and the isDefined function can’t find it. Is that a normal behavior?

Here an example :

<cfset myStructure = structNew()>
<cfset myStructure.1 = "Hello there">

<cfdump var="#myStructure#" label="myStructure">

<cfif isDefined("myStructure.1")>
    The variable myStructure.1 is defined
<cfelse>
    The variable myStructure.1 is not defined <!--- It returns is not defined --->
</cfif>



<hr />

<cfset myStructure = structNew()>
<cfset myStructure.Item1 = "Hello there">

<cfdump var="#myStructure#" label="myStructure">

<cfif isDefined("myStructure.Item1")>
    The variable myStructure.Item1 is defined <!--- It returns is defined --->
<cfelse>
    The variable myStructure.Item1 is not defined
</cfif>

Should I report a bug?

Thank you

OS: Windows Server 2016 (10.0) 64bit
Java Version: 1.8.0_181 (Oracle Corporation) 64bit
Tomcat Version: Apache Tomcat/8.5.33
Lucee Version: Lucee 5.3.8.159-SNAPSHOT

Only e tip: with “StructKeyExists” fork fine.
:slight_smile:

it’s deprecated :slight_smile:

1 Like

Hi @Roberto_Marzialetti,

Yes I know, I’m using it. But I want to pass a path of a structure of structure of structure… to a component and verify inside the component that this variable exists, then returns it’s value or a default value. It look simple with isDefined, but it doesn’t work if the path have a numeric key. And with StructKeyExists, I have to nest multiple StructKeyExists to verify that the final key exists.

Hi @Zackster,

Oh… this explains that.

Thank you for the info!

check out these options instead

3 Likes

Interesting! I’ll keep it in mind. Thank you!

This should not encourage you to use isdefined(), but for structs you can still always use the bracket notation as a working solution. Just to complete all the alternatives here:

<cfset myStructure = structNew()>
<cfset myStructure["1"] = "Hello there">

<cfdump var="#myStructure#" label="myStructure">

<cfif isDefined("myStructure['1']")>
    The variable myStructure.1 is defined
<cfelse>
    The variable myStructure.1 is not defined <!--- It returns is not defined --->
</cfif>



<hr />

<cfset myStructure = structNew()>
<cfset myStructure["Item1"] = "Hello there">

<cfdump var="#myStructure#" label="myStructure">

<cfif isDefined("myStructure['Item1']")>
    The variable myStructure.Item1 is defined <!--- It returns is defined --->
<cfelse>
    The variable myStructure.Item1 is not defined
</cfif>


I’m a little devastated that isdefined() is depracated. It seems such a simple and useful function.

What would be the sensible way of handling this…

<cfif isdefined("url.customer")>
  do some stuff here
</cfif>

Do you just replace the isdefined() with structkeyexists(url,“customer”) or is there a better way?

I’d be interested to know why it was depracated, if anyone knows.

Simon

StructKeyExists( url,“customer” ) is simply more efficient, especially under load

Imagine you are Lucee, and how you are going to execute that statement.

IsDefined

  1. does some string parsing
  2. figures out it’s an url variable
  3. then it’s going to do a structKeyExists on url to see if customer exists.

While structKeyExists only does step 3.

Just my 2 cents. One of the reasons I LOVE CFML is its natural language oriented and its very simple on the surface and allows you to plumb and extend the depths of it as programming skills develop.

To me:
isDefined is natural language orientated.
strucyKeyExists is not natural language oriented.

I like the natural language style tags, ColdFusion is littered with them, and it serves as a way for people who didn’t spend years honing their programming skills to create simple to complex applications.

2 Likes

@Simon_Goldschmidt that any sruff is marked as deprecated doesn’t mean you can’t use it. I still have some legacy code with tons of isdefined() in it and I won’t change it (for now()). Its more a business decision, because my fellow here (who is not a good programmer) needs to understand the steps and all the logic. He only knows legacy cfml stuff and no OOP.

Please read the following information to the definition “deprecated”
Deprecated Tags & Functions :: Lucee Documentation

@Terry_Whitney the reason you love cfml is the reason I’ve started with the language. I remember reading about cfloop in a Forta book, when I only knew loops with for/next commands from BASIC. There have been tons of such easy working tags. Love it!

2 Likes

ah good old basic, who doesn’t miss GOTO statements?

My first puter was CP/M box, with MBASIC, I had to figure out how to move the cursor around and clear the screen by poking at bits of memory so I could write some games

2 Likes

My very first computer was a TSR-color. My parents handed me a programming book and said here, you want video games, there they are. I was six at the time… 0 - CLS - 5 Beep (sound 1,10) 10 Go to 5 :slight_smile:

1 Like

Nice age to get an programming book. I remember the look&feel&smell of those books.

These computers have been out earlier than mine :smiley: : I began with a TK85 (16kb memory), it was a brazilian clone of the ZX81. Had to load the software from on old school tape recorder playing those compact cassettes. I was always crossing fingers to not see an error after the load. Once I got a programmer’s magazine which had a “space invaders” game code printed as a list of hex. You needed to write a small basic programm first in order to add the list bytes to the memory. Then
I played like forever, because as soon as you switched the power off, everything was gone.

1 Like

I remember those tape recorders… i did not have one until I was 8. My mother kept unplugging my computer and there was no storage, so in many cases, days of work would get destroyed.