• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Test for specific array position?

Engaged ,
Aug 13, 2013 Aug 13, 2013

Copy link to clipboard

Copied

Hi

I have a 2 dimensional array stored in sessions scope:

     <cfset Session.POarCart=#arrayNew(2)#>

and it can have as many as 7 elements (for example):

     Session.POarCart[1][1]

     Session.POarCart[1][2]

     Session.POarCart[1][3]

     Session.POarCart[1][4]

     Session.POarCart[1][5]

     Session.POarCart[1][6]

     Session.POarCart[1][7]

... however elements 6 and 7 (Session.POarCart[1][6] and Session.POarCart[1][7]) don't always get filled (or populated, or set, whatever the nomenclature....)

Does anyone know how to check if a specific array element "contains something"? "is not null"? "has length"? I can test that the array exists, and it does, but I don't know how to test for the exact position of the array.

My code is failing when I attempt to reference Session.POarCart[1][6] because it doesn't contain anything.

Anyone?

Thanks,

Rich

Views

1.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Aug 13, 2013 Aug 13, 2013

Copy link to clipboard

Copied

Rich,

You could use ArrayLen() to find the length of your array.  If you find that a 6th or 7th element exists, you'd still have to check the value to see if they are empty or not.

-Carl V.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 13, 2013 Aug 13, 2013

Copy link to clipboard

Copied

Carl

arrayLen() only gives me the length of the entire array, it does not tell me how long each array's "entry" is. For example,

<cfset arrayLength=arrayLen(Session.POarCartEDIT)>

arraylength: #arrayLength#<br />

Displays "2".

Each array entry can have up to 7 positions; what I need is to know inside of each of the 2 array entries, is there a value of [1][6], or [1][7].

How do you do that?

Thanks,

Rich

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Aug 13, 2013 Aug 13, 2013

Copy link to clipboard

Copied

Right, so you have an Array inside of an Array.  So <cfset arrayLength=ArrayLength(Session.POarCart[1])>.  Assuming your Session.POarCart array has multiple arrays inside, you would probably wrap the logic inside a loop, and loop over the outer array.

<cfset innerArrayLength = 0>

<cfloop index="i" from="1" to="#ArrayLen(Session.POarCart)#">

     <cfset innerArrayLength = ArrayLen(Session.POarCart)>

     <!--- DO STUFF HERE --->

</cfloop>

HTH,

-Carl V.

Message was edited by: Carl Von Stetten

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 13, 2013 Aug 13, 2013

Copy link to clipboard

Copied

Carl

Thanks your post was helpful.

So what function would you use to determine if one of the inner loop's array contains a value? I'm still unable to gleen that value....

Thanks again,

Rich

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Aug 13, 2013 Aug 13, 2013

Copy link to clipboard

Copied

If you're talking about positions 6 & 7, once you've verified that the length of the array is greater than 5, you could use Len() to check if the value is empty.

Just out of curiosity, since the inner array can also be of variable length, are you doing the same operation on each of the array's elements?  Or do you do something special with element 6 and 7 if they are present?

-Carl V.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 14, 2013 Aug 14, 2013

Copy link to clipboard

Copied

ArrayIsDefined should do what you need. 

<cfloop index="i" from="1" to="#arrayLen(Session.POarCart[1])#">

  <cfif ArrayIsDefined(Session.POarCart[1], i)>

    ...

  </cfif>

</cfloop>

OR, you may be better looping like so:

<cfloop index="item" array="#Session.POarCart[1]#">

   #item#

</cfloop>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 14, 2013 Aug 14, 2013

Copy link to clipboard

Copied

Thank you, very helpful.

Regards,

Rich

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Aug 14, 2013 Aug 14, 2013

Copy link to clipboard

Copied

LATEST

Oooohhh!  Nice one.  Somehow I was completely unaware of ArrayIsDefined.

-Carl V.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 14, 2013 Aug 14, 2013

Copy link to clipboard

Copied

What I'd like to know is why the hell you're using a two-dimensional array for what seems to be a shopping cart. It should be an array of structs, surely?

--

Adam

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 14, 2013 Aug 14, 2013

Copy link to clipboard

Copied

...previously stated business requirements that I inherited. Didn't know we had to present the entire use case for help on this forum.

Your response was not helpful at all.

Regards,

Rich

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 14, 2013 Aug 14, 2013

Copy link to clipboard

Copied

How am I to know you're maintaining existing code, not writing new code?

Because if you are writing new code, then the approach is likely to be a substandard one, so whilst you're struggling with it, now might be the perfect moment to take a step back and have a closer look at a good solution.

Pull your head in.

--

Adam

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation