velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jean Francois Chamard <>
Subject Re: finding the length of an array
Date Thu, 18 Aug 2005 13:42:57 GMT
Hi Chris,
    perhaps I am mistaken but even in Velocity 1.5 dev, you can't find 
the length of an array with
$array.length so your length (in velocity) is probably null

I use an object which I put in my velocity context to get the length of 
an array,
here is the object if you are interested:

    public class ArrayUtil {
        public ArrayUtil() {
        public Integer length(Object object)
            if (object == null || !object.getClass().isArray())
                return null;

            return new Integer(Array.getLength(object));

I found something similar in the velocity mailling archive before I 
wrote the class above.
Then in velocity, if you give the class the name arrayUtil, you call it 
that way..


this will return the good length
I hope it helps
Jean-Francois Chamard

Townson, Chris wrote:

>after reading through Jean Francois Chamard's excellent technique for
>finding the last item in an array with Velocity (posted 22/07/2005), I've
>been trying to implement this for a array of HTML links.
>however, I've been encountering problems using the length method which is
>used there: it's just failing silently (haven't got access to the velocity
>log for this at present, unfortunately).
>here is the code I am using:
>## a load of links, with text + hrefs etc held in their own array
>#set ( $link1 = ["link 1","this is link one",""] )
>#set ( $link2 = ["link 2","this is link two","http://link.two"] )
>#set ( $link3 = ["link 3","this is link three","http://link.three"] )
>#set ( $link4 = ["link 4","this is link four","http://link.four"] )
>#set ( $link5 = ["link 5","this is link five","http://link.five"] )
>#set ( $link6 = ["link 6","this is link six","http://link.six"] )
>## stick alll those arrays into an array
>#set ( $linkMultiArray = [$link1,$link2,$link3,$link4,$link5,$link6] )
>## do loop to produce output
>#set( $len = $linkMultiArray.length )
>#foreach($link in $linkMultiArray)
>    #set( $velocityCount2 = $velocityCount + 1 )
>    #set( $len2 = $len - 1 )
>    $link.get(0)#if($len2 == $velocityCount) and#elseif($len >
>$velocityCount2), #end
>the desired result:
>link 1, link 2, link 3, link 4, link 5 and link 6
>what I'm getting:
>link 1link 2link 3link 4link 5link 6
>Any comments/suggestions/help/answers gratefully received!
>DISCLAIMER: This e-mail is confidential and should not be used by anyone who is
>not the original intended recipient. If you have received this e-mail in error
>please inform the sender and delete it from your mailbox or any other storage
>mechanism. Neither Macmillan Publishers Limited nor any of its agents accept
>liability for any statements made which are clearly the sender's own and not
>expressly made on behalf of Macmillan Publishers Limited or one of its agents.
>Please note that neither Macmillan Publishers Limited nor any of its agents
>accept any responsibility for viruses that may be contained in this e-mail or
>its attachments and it is your responsibility to scan the e-mail and 
>attachments (if any). No contracts may be concluded on behalf of Macmillan 
>Publishers Limited or its agents by means of e-mail communication. Macmillan 
>Publishers Limited Registered in England and Wales with registered number 785998 
>Registered Office Brunel Road, Houndmills, Basingstoke RG21 6XS   

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message