Skip to main content
Inspiring
August 13, 2013
Question

Test for specific array position?

  • August 13, 2013
  • 11 replies
  • 1716 views

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

    This topic has been closed for replies.

    11 replies

    Inspiring
    August 14, 2013

    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

    Inspiring
    August 14, 2013

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

    Inspiring
    August 14, 2013

    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

    Carl Von Stetten
    Legend
    August 13, 2013

    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.

    Inspiring
    August 13, 2013

    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

    Carl Von Stetten
    Legend
    August 13, 2013

    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