does not seems to be working thou
# cfml-general
s
does not seems to be working thou
m
What happens that makes you think it's not working? You might need to use
arrayIsDefined()
instead
s
so arrayisDefined(array[2]), because it could be an empty array, or array has only 1 attribute
m
Why not dump the entire array and see?
You should probably look up the documentation for the syntax
s
isDefined('array[1]')
is not going to cause a breaking error. If it’s not defined it will return
false
. If it is defined it will return
true
. e.g.
isDefined('wobblybobly[345]')
will return
false
if you have never defined it. can you explain what you are trying to solve? Is it any of these? - is the array defined - is there one element in the array - is the first element of the array something you expect to be defined If we know more specifically what you are attempting to solve we may be able to help. And as previously recommended, it will help when you look up the documentation for the syntax.
a
@Simone you really need to get used to reading the documentation when you have a problem with your code (I also invite other ppl to use that as their first recommendation for guidance for questions like "my code doesn't work why?"). https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-in-k/isdefined.html - what does it say about passing it an array reference? Similarly with your attempted usage of
arrayIsDefined
. When it didn't work, did you look at the docs? No. Always do that first before asking anyone else for help: https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-a-b/arrayisdefined.html
s
@steveduke all i am trying if its defined, use it, if not exists or defined but empty or defined but not for that key which array[2], it has only 1, it should return me 0 else it should return me that number
i tried like this
Copy code
<cfset s = []>
<cfset x = isArray(s) AND !ArrayIsEmpty(s[2]) ? s[2] : 0>
<cfdump var="#x#">
but it is erroring out
m
What's the error say? I don't think
arrayIsEmpty()
does what you are expecting it to do.
Again - check the documentation first.
s
can't use arrayisDefined, it needs an index
m
arrayIsDefined(s,2)
2 is your index, in this case.
e
@Adam Cameron Naw its way easier to just post in the channel all your problems and expect someone else to debug it. For example, I have a 1985 toyota pickup with a bad starter. Sure, I could read the car repair manual and eventually put a new starter in it, but instead its far better to just scream into the wilderness hoping a random mechanic will show up and just make it work.
πŸ”¨ 1
πŸ˜„ 1
t
@steveduke completely off topic but I used to work with a wobblybob lovely bloke.
πŸ˜‚ 1
s
assuming your array exists, maybe this is what you are asking?
Copy code
<cfscript>
myArray = [1,2,3,4];
x = isNumeric(myArray?.1)?myArray[1]:0;
echo(x);
</cfscript>
t
@steveduke safe navigation doesn't work for arrays in ACF but you can fall back to the underlying java object https://trycf.com/gist/b6728483ae6170cc9025aa5f0adb2fe2/lucee5?theme=monokai
s
ah yes, maybe something as ridiculous as this?
Copy code
writeOutput(!myArray.len()?0:isNumeric(myArray[1])?myArray[1]:0);
a
There doesn't need to be any of that dicking around trying to be clever. If one wants to know if array
a
has element
i
, one just needs to call
arrayIsDefined(a, i)
. It's well documented and there's no mystery to it. One just has to take the minimum amount of time learning one's craft.
πŸ˜‚ 2
s
<cfscript> myArray = ''; writeOutput(isArray(myArray) && myArray.isEmpty()); </cfscript> What else do you need to check an array?
m
OP seems to want to check individual indices, not the overall array.
s
<cfscript> myArray = [1,2,3]; writeOutput(isArray(myArray) && !myArray.isEmpty()) && myArray.len() == 3 && isDefined(myArray[3]); </cfscript>
m
Please read the thread. IsDefined() does not work in this context.
s
<cfscript> myArray = [1,2,3]; i = 999; // fake a counter writeOutput(isArray(myArray) && !myArray.isEmpty() && myArray.len() >= i); </cfscript>
a
@spills why are you returning to a thread that was answered / finished last week? Also, if yer gonna post code, learn how to format it pls. one back tick if it's inline
like this
. Three back-ticks if it's a block:
Copy code
like this
s
it was a small piece of code .... but yeah the formatting would have been nicer. I got a response ... but yeah I didn't even notice popped up in unreads on older instance.
Refreshed now!