How to get the Array/List count?

  • Hi

    I have some problems with an actual array. I do not get the count of it.
    Here is the code of the array and the function to read it.
    I do get the product and color value out of it.

    What is wrong there?

    Code
    <array name="cart99">	<item name="i0" product="product1" color="red" url="123"/>	<item name="i1" product="product2" color="blue"/><item name="i2" product="product3" color="green"/>	</array>
    	<action name="test">	trace(cart99.count);
    		trace(array[cart99].item[i1].product);trace(array[cart99].item[0].color);	</action>

    PS: How can i format the code properly?

    Thanks
    Sven

  • Hi Sven,


    Your array should be:
    <array name="cart99">
    <sub name="item0" product="product1" color="red" url="123" />
    <sub name="item1" product="product2" color="blue" url="231" />
    <sub name="item2" product="product3" color="green" url="321" />
    </myarray >

    array count of sub-items:
    trace(array[cart99].sub.count);

    get a value of array:
    trace(array[cart99].sub[item0].product);

    With a quick for() Action you can get all the infos of your array:

    for(set(i, 0), i LE array[cart99].sub.count, inc(i),
    txtadd(myTargetItem, 'item', get(i) );
    trace(array[cart99].sub[get(myTargetItem)].product );
    trace(array[cart99].sub[get(myTargetItem)].color );
    trace(array[cart99].sub[get(myTargetItem)].url );
    );

    NOT tested, but should work...

    Best,
    Steve

    Edited once, last by svb (May 2, 2017 at 4:48 PM).

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!