You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by James Cook <ji...@iname.com> on 2001/11/20 04:51:34 UTC

Accessing the length of an array

Is there any way to access the length of an array?
 
I have tried ${a.length} and many others, but was unsuccessful.
 
-jim



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Accessing the length of an array

Posted by David Rees <dr...@runt.ebetinc.com>.
On Tue, Nov 20, 2001 at 06:15:46AM -0500, Geir Magnusson Jr. wrote:
> On 11/19/01 11:33 PM, "David Rees" <dr...@runt.ebetinc.com> wrote:
> 
> > On Mon, Nov 19, 2001 at 10:51:34PM -0500, James Cook wrote:
> >> Is there any way to access the length of an array?
> >>  
> >> I have tried ${a.length} and many others, but was unsuccessful.
> > 
> > $a.length() should do the trick.
> > 
> 
> Actually, I don't think it will.
> 
> For an array (i.e.  String[] ), 'length' is a public member, not a method,
> and Velocity doesn't allow access to public members.
> 
> (Here's a real place where this is a drag, I confess...)
> 
> If you have lots of arrays that you are working with already, you could just
> put a java.lang.reflect.Array in the context and use that as a tool to
> manipulate your arrays.

Argh, you're right.  Teaches me right for posting without thinking right
before I go home.  ;-)

I was thinking array as in an ArrayList object or something which implements
the List interface.  Had it completely wrong, since that interface uses
size(), not length().

In this case, I would recommend mapping the array to an ArrayList or
something similar.

-Dave

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Accessing the length of an array

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 11/20/01 6:52 AM, "Attila Szegedi" <sz...@freemail.hu> wrote:

> 
> ----- Original Message -----
> From: "Geir Magnusson Jr." <ge...@optonline.net>
> To: <ve...@jakarta.apache.org>
> Sent: 2001. november 20. 12:15
> Subject: Re: Accessing the length of an array
> 
> 
>> On 11/19/01 11:33 PM, "David Rees" <dr...@runt.ebetinc.com> wrote:
>> 
>>> On Mon, Nov 19, 2001 at 10:51:34PM -0500, James Cook wrote:
>>>> Is there any way to access the length of an array?
>>>> 
>>>> I have tried ${a.length} and many others, but was unsuccessful.
>>> 
>>> $a.length() should do the trick.
>>> 
>> 
>> Actually, I don't think it will.
>> 
>> For an array (i.e.  String[] ), 'length' is a public member, not a method,
> 
> This is not true, either. Arrays don't have a 'length' member. The ".length"
> on array is an artificial idiom in Java language that translates to
> 'arraylength' (0xbe) bytecode instruction for popping an array from the top
> of stack and pushing its length on it...

I knew I was misrepresenting it, but thanks for the info - I wasn't aware
how it worked...

 
> This is not the only example of constructs in Java that look like a member,
> but they aren't. Another prominent example is 'super': no object has a
> member called 'super', yet in Java language you can prefix a method call
> with "super." to indicate that a method should be invoked with
> 'invokespecial' instead of the normal 'invokevirtual' bytecode.


-- 
Geir Magnusson Jr.                       geirm@optonline.net
System and Software Consulting
You're going to end up getting pissed at your software
anyway, so you might as well not pay for it. Try Open Source.



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Accessing the length of an array

Posted by Attila Szegedi <sz...@freemail.hu>.
Here's a complete example.
This minimalistic class:


public class Class1
{
    private void x()
    {
        System.err.println(String.class);
        System.err.println(Object.class);
    }
}


will translate into (as decompiled by Jad):


public class Class1
{
    private static Class class$java$lang$String; /* synthetic field */
    private static Class class$java$lang$Object; /* synthetic field */

    private static Class class$(String s)
    {
        try
        {
            return Class.forName(s);
        }
        catch(ClassNotFoundException e)
        {
            throw new NoClassDefFoundError(e.getMessage());
        }
    }

    private void x()
    {
        System.err.println(
            class$java$lang$String == null
            ? class$java$lang$String = class$("java.lang.String")
            : class$java$lang$String
        );
        System.err.println(
            class$java$lang$Object == null
            ? class$java$lang$Object = class$("java.lang.Object")
            : class$java$lang$Object
        );
    }
}

Attila.

----- Original Message -----
From: "Attila Szegedi" <sz...@freemail.hu>
To: "Velocity Users List" <ve...@jakarta.apache.org>;
<pa...@krankikom.de>
Sent: 2001. november 20. 13:41
Subject: Re: Accessing the length of an array


>
> ----- Original Message -----
> From: "Paulo Gaspar" <pa...@krankikom.de>
> To: "Velocity Users List" <ve...@jakarta.apache.org>
> Sent: 2001. november 20. 13:12
> Subject: RE: Accessing the length of an array
>
>
> > UAU Attila! You sure know your bytecodes!
> >
> > Question: what about that "class member", as in "HashMap.class"
> > or "SomeOtherClass.class"?
> > (I was always curious about this one!)
> >
>
> I was, too so I once looked at how it looks in a compiled class, and it's
> UGLY. I'd say, you don't want to know. If you don't heed the warning and
> really want to get horrified, read on...
>
> When the FooClass source code contains the "org.foo.BarClass.class"
> expression, the compiler will:
>
> - create an uninitialized private static field in FooClass named
> org$foo$BarClass or something similar (it's compiler dependent)
> - replace every occurrence of "BarClass.class" with org$foo$BarClass, AND
> - prepend every occurrence of the expression with
>   if(org$foo$BarClass == null) org$foo$BarClass =
> Class.forName("org.foo.BarClass");
>
> Oh, and I didn't mention the additional try/catch logic for translating
> ClassNotFoundException into NoClassDefFoundError. At this point you really
> start to appreciate the pains of people who write Java decompilers...
>
> Note that since $ is a valid identifier character in Java VM, but not
valid
> in Java language there is no possibility of name clash - it's commonly
used
> for synthetic identifiers (nested classes, package level accessors to
> private methods for inner classes, etc.).
>
> Attila.
>
> >
> > Have fun,
> > Paulo
> >
> > http://www.krankikom.de
> > http://www.ruhronline.de
> >
>
>
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Accessing the length of an array

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 11/20/01 7:53 AM, "Attila Szegedi" <sz...@freemail.hu> wrote:

> Hm. you're right. I've just tried adding an "int a$b" field to one of my
> classes, and it compiled smoothly...

Ew.  Yech :)

> Attila.
> 
> ----- Original Message -----
> From: "Geir Magnusson Jr." <ge...@optonline.net>
> To: <ve...@jakarta.apache.org>
> Sent: 2001. november 20. 13:47
> Subject: Re: Accessing the length of an array
> 
> 
>> On 11/20/01 7:41 AM, "Attila Szegedi" <sz...@freemail.hu> wrote:
>> 
>>> Note that since $ is a valid identifier character in Java VM, but not
> valid
>>> in Java language there is no possibility of name clash - it's commonly
> used
>>> for synthetic identifiers (nested classes, package level accessors to
>>> private methods for inner classes, etc.).
>> 
>> Is that really true?  I vaguely remember from the JLS that $ is valid for
>> identifiers, but included for legacy/historical reasons...
>> 
>> I have never tried to use it, so I don't know....
>> 
>> 
>> --
>> Geir Magnusson Jr.     geirm@optonline.net
>> System and Software Consulting
>> "Whoever would overthrow the liberty of a nation must begin by subduing
> the
>> freeness of speech." - Benjamin Franklin
>> 
>> 
>> 
>> --
>> To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
>> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
>> 
>> 
> 
> 
> --
> To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
> 

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
Be a giant.  Take giant steps.  Do giant things...


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Accessing the length of an array

Posted by Attila Szegedi <sz...@freemail.hu>.
Hm. you're right. I've just tried adding an "int a$b" field to one of my
classes, and it compiled smoothly...
Attila.

----- Original Message -----
From: "Geir Magnusson Jr." <ge...@optonline.net>
To: <ve...@jakarta.apache.org>
Sent: 2001. november 20. 13:47
Subject: Re: Accessing the length of an array


> On 11/20/01 7:41 AM, "Attila Szegedi" <sz...@freemail.hu> wrote:
>
> > Note that since $ is a valid identifier character in Java VM, but not
valid
> > in Java language there is no possibility of name clash - it's commonly
used
> > for synthetic identifiers (nested classes, package level accessors to
> > private methods for inner classes, etc.).
>
> Is that really true?  I vaguely remember from the JLS that $ is valid for
> identifiers, but included for legacy/historical reasons...
>
> I have never tried to use it, so I don't know....
>
>
> --
> Geir Magnusson Jr.     geirm@optonline.net
> System and Software Consulting
> "Whoever would overthrow the liberty of a nation must begin by subduing
the
> freeness of speech." - Benjamin Franklin
>
>
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Accessing the length of an array

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 11/20/01 7:41 AM, "Attila Szegedi" <sz...@freemail.hu> wrote:

> Note that since $ is a valid identifier character in Java VM, but not valid
> in Java language there is no possibility of name clash - it's commonly used
> for synthetic identifiers (nested classes, package level accessors to
> private methods for inner classes, etc.).

Is that really true?  I vaguely remember from the JLS that $ is valid for
identifiers, but included for legacy/historical reasons...

I have never tried to use it, so I don't know....


-- 
Geir Magnusson Jr.     geirm@optonline.net
System and Software Consulting
"Whoever would overthrow the liberty of a nation must begin by subduing the
freeness of speech." - Benjamin Franklin



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Accessing the length of an array

Posted by Attila Szegedi <sz...@freemail.hu>.
----- Original Message -----
From: "Paulo Gaspar" <pa...@krankikom.de>
To: "Velocity Users List" <ve...@jakarta.apache.org>
Sent: 2001. november 20. 13:12
Subject: RE: Accessing the length of an array


> UAU Attila! You sure know your bytecodes!
>
> Question: what about that "class member", as in "HashMap.class"
> or "SomeOtherClass.class"?
> (I was always curious about this one!)
>

I was, too so I once looked at how it looks in a compiled class, and it's
UGLY. I'd say, you don't want to know. If you don't heed the warning and
really want to get horrified, read on...

When the FooClass source code contains the "org.foo.BarClass.class"
expression, the compiler will:

- create an uninitialized private static field in FooClass named
org$foo$BarClass or something similar (it's compiler dependent)
- replace every occurrence of "BarClass.class" with org$foo$BarClass, AND
- prepend every occurrence of the expression with
  if(org$foo$BarClass == null) org$foo$BarClass =
Class.forName("org.foo.BarClass");

Oh, and I didn't mention the additional try/catch logic for translating
ClassNotFoundException into NoClassDefFoundError. At this point you really
start to appreciate the pains of people who write Java decompilers...

Note that since $ is a valid identifier character in Java VM, but not valid
in Java language there is no possibility of name clash - it's commonly used
for synthetic identifiers (nested classes, package level accessors to
private methods for inner classes, etc.).

Attila.

>
> Have fun,
> Paulo
>
> http://www.krankikom.de
> http://www.ruhronline.de
>



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Accessing the length of an array

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 11/20/01 7:12 AM, "Paulo Gaspar" <pa...@krankikom.de> wrote:

> UAU Attila! You sure know your bytecodes!
> 

+1


> Question: what about that "class member", as in "HashMap.class"
> or "SomeOtherClass.class"?
> (I was always curious about this one!)
> 
> 
> Have fun,
> Paulo
> 
> http://www.krankikom.de
> http://www.ruhronline.de
> 
>> -----Original Message-----
>> From: Attila Szegedi [mailto:szegedia@freemail.hu]
>> Sent: Tuesday, November 20, 2001 12:52 PM
>> 
>> ----- Original Message -----
>> From: "Geir Magnusson Jr." <ge...@optonline.net>
>> To: <ve...@jakarta.apache.org>
>> 
>>> On 11/19/01 11:33 PM, "David Rees" <dr...@runt.ebetinc.com> wrote:
>>> 
>>> ...
>>> 
>>> For an array (i.e.  String[] ), 'length' is a public member,
>> not a method,
>> 
>> This is not true, either. Arrays don't have a 'length' member.
>> The ".length"
>> on array is an artificial idiom in Java language that translates to
>> 'arraylength' (0xbe) bytecode instruction for popping an array
>> from the top
>> of stack and pushing its length on it...
>> 
>> This is not the only example of constructs in Java that look like
>> a member,
>> but they aren't. Another prominent example is 'super': no object has a
>> member called 'super', yet in Java language you can prefix a method call
>> with "super." to indicate that a method should be invoked with
>> 'invokespecial' instead of the normal 'invokevirtual' bytecode.
>> 
>> Attila.
>> 
>> ...
> 
> --
> To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
> 

-- 
Geir Magnusson Jr.                       geirm@optonline.net
System and Software Consulting
You're going to end up getting pissed at your software
anyway, so you might as well not pay for it. Try Open Source.



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: Accessing the length of an array

Posted by Paulo Gaspar <pa...@krankikom.de>.
UAU Attila! You sure know your bytecodes!

Question: what about that "class member", as in "HashMap.class"
or "SomeOtherClass.class"? 
(I was always curious about this one!)


Have fun,
Paulo

http://www.krankikom.de
http://www.ruhronline.de
 
> -----Original Message-----
> From: Attila Szegedi [mailto:szegedia@freemail.hu]
> Sent: Tuesday, November 20, 2001 12:52 PM
> 
> ----- Original Message -----
> From: "Geir Magnusson Jr." <ge...@optonline.net>
> To: <ve...@jakarta.apache.org>
> 
> > On 11/19/01 11:33 PM, "David Rees" <dr...@runt.ebetinc.com> wrote:
> >
> > ...
> >
> > For an array (i.e.  String[] ), 'length' is a public member, 
> not a method,
> 
> This is not true, either. Arrays don't have a 'length' member. 
> The ".length"
> on array is an artificial idiom in Java language that translates to
> 'arraylength' (0xbe) bytecode instruction for popping an array 
> from the top
> of stack and pushing its length on it...
> 
> This is not the only example of constructs in Java that look like 
> a member,
> but they aren't. Another prominent example is 'super': no object has a
> member called 'super', yet in Java language you can prefix a method call
> with "super." to indicate that a method should be invoked with
> 'invokespecial' instead of the normal 'invokevirtual' bytecode.
> 
> Attila.
>
> ...

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Accessing the length of an array

Posted by Attila Szegedi <sz...@freemail.hu>.
----- Original Message -----
From: "Geir Magnusson Jr." <ge...@optonline.net>
To: <ve...@jakarta.apache.org>
Sent: 2001. november 20. 12:15
Subject: Re: Accessing the length of an array


> On 11/19/01 11:33 PM, "David Rees" <dr...@runt.ebetinc.com> wrote:
>
> > On Mon, Nov 19, 2001 at 10:51:34PM -0500, James Cook wrote:
> >> Is there any way to access the length of an array?
> >>
> >> I have tried ${a.length} and many others, but was unsuccessful.
> >
> > $a.length() should do the trick.
> >
>
> Actually, I don't think it will.
>
> For an array (i.e.  String[] ), 'length' is a public member, not a method,

This is not true, either. Arrays don't have a 'length' member. The ".length"
on array is an artificial idiom in Java language that translates to
'arraylength' (0xbe) bytecode instruction for popping an array from the top
of stack and pushing its length on it...

This is not the only example of constructs in Java that look like a member,
but they aren't. Another prominent example is 'super': no object has a
member called 'super', yet in Java language you can prefix a method call
with "super." to indicate that a method should be invoked with
'invokespecial' instead of the normal 'invokevirtual' bytecode.

Attila.

> and Velocity doesn't allow access to public members.
>
> (Here's a real place where this is a drag, I confess...)
>
> If you have lots of arrays that you are working with already, you could
just
> put a java.lang.reflect.Array in the context and use that as a tool to
> manipulate your arrays.
>
> Geir
>
>
> --
> Geir Magnusson Jr.                       geirm@optonline.net
> System and Software Consulting
> You're going to end up getting pissed at your software
> anyway, so you might as well not pay for it. Try Open Source.
>
>
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Accessing the length of an array

Posted by Jim Rudnicki <jd...@pacbell.net>.
> >I think if a.length==0, then the array a should not be put in the
context.
> >Keep logic out of the view.  Either a exists and should be shown, or it
does
> >not exist and should not be displayed.
>
> And so you force an #if statement around a loop?
>
> What is simpler:
>
> #if($!foo)
> #for ($key in $foo)
>  ... display ...
> #end
> #end
>
> or just
>
> #for ($key in $foo)
>  ... display ...
> #end
>
> with zero iterations if the array.length == 0 ?

In many real examples I want #1.

#if($!foo)
<table>
#for ($key in $foo)
<tr> ... display ...</tr>
#end
</table>
#end

#2 produces just creates an empty table which isn't right:

<table>
#for ($key in $foo)
<tr> ... display ... </tr>
#end
</table>

Jim


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Accessing the length of an array

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 11/21/01 7:03 AM, "Henning P. Schmiedehausen" <ma...@hometree.net>
wrote:

> Jim Rudnicki <jd...@pacbell.net> writes:
> 
>>>> One dumb question: For what do you _need_ the length? Velocity gives
>>>> you an implicit iterator for arrays with #foreach()
>>> 
>>> #if (a.length == 0)
> 
>> I think if a.length==0, then the array a should not be put in the context.
>> Keep logic out of the view.  Either a exists and should be shown, or it does
>> not exist and should not be displayed.
> 
> And so you force an #if statement around a loop?
> 
> What is simpler:
> 
> #if($!foo)
> #for ($key in $foo)
> ... display ...
> #end
> #end
> 
> or just
> 
> #for ($key in $foo)
> ... display ...
> #end
> 
> with zero iterations if the array.length == 0 ?
> 

I'll take curtain #2...

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
"They that can give up essential liberty to obtain a little temporary safety
deserve neither liberty nor safety." - Benjamin Franklin



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Accessing the length of an array

Posted by "Henning P. Schmiedehausen" <ma...@hometree.net>.
Jim Rudnicki <jd...@pacbell.net> writes:

>> > One dumb question: For what do you _need_ the length? Velocity gives
>> > you an implicit iterator for arrays with #foreach()
>>
>> #if (a.length == 0)

>I think if a.length==0, then the array a should not be put in the context.
>Keep logic out of the view.  Either a exists and should be shown, or it does
>not exist and should not be displayed.

And so you force an #if statement around a loop?

What is simpler:

#if($!foo)
#for ($key in $foo)
 ... display ...
#end
#end

or just

#for ($key in $foo)
 ... display ...
#end

with zero iterations if the array.length == 0 ?

	Regards
		Henning







>--
>To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
>For additional commands, e-mail: <ma...@jakarta.apache.org>

-- 
Dipl.-Inf. (Univ.) Henning P. Schmiedehausen       -- Geschaeftsfuehrer
INTERMETA - Gesellschaft fuer Mehrwertdienste mbH     hps@intermeta.de

Am Schwabachgrund 22  Fon.: 09131 / 50654-0   info@intermeta.de
D-91054 Buckenhof     Fax.: 09131 / 50654-20   

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Accessing the length of an array

Posted by Jim Rudnicki <jd...@pacbell.net>.
> > One dumb question: For what do you _need_ the length? Velocity gives
> > you an implicit iterator for arrays with #foreach()
>
> #if (a.length == 0)

I think if a.length==0, then the array a should not be put in the context.
Keep logic out of the view.  Either a exists and should be shown, or it does
not exist and should not be displayed.

Jim








--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Accessing the length of an array

Posted by Barbara Baughman <ba...@utdallas.edu>.
I typically handle what the designer needs to know about the count in the
logic of the servlet and send it to the template as a context field.  I
send numbers as a String, since Context doesn't like int's.  That works
fine to display the count.  Or I send a field that may have "none" or
"some" in it.  Something easy for a designer to work with.

Barbara Baughman
X2157

On Tue, 20 Nov 2001, Stephane MOR wrote:

> 
> ----- Original Message ----- 
> From: "James Cook" <ji...@iname.com>
> > > One dumb question: For what do you _need_ the length? Velocity gives
> > > you an implicit iterator for arrays with #foreach()
> > 
> > #if (a.length == 0)
> 
> Hi,
> 
> I guess you could do :
> #set ( $i = 0)
> #foreach( $foo in $bar )
>     #set($i = $i + 1)
> #end
> #if ($i == 0) 
>     The array is empty
> #else
>     The array contains elements
> #end
> which is quite ugly, but works ...
> 
> 
> > 
> > -jim
> > 
> > --
> 
> 
> 
> _________________________________________________________
> Do You Yahoo!?
> Get your free @yahoo.com address at http://mail.yahoo.com
> 
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
> 
> 


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: Accessing the length of an array

Posted by James Cook <ji...@iname.com>.
Yeah, that was my current workaround. I would rather expose a method in my
context (using WebWork) like:

	public int getArrayLength() {
		return a.length;
	}

or use the other recommendation to use the Arrays class.

-jim

> -----Original Message-----
> From: Stephane MOR [mailto:stephanemor@yahoo.fr]
> Sent: Tuesday, November 20, 2001 12:36 PM
> To: Velocity Users List
> Subject: Re: Accessing the length of an array
>
>
>
> ----- Original Message -----
> From: "James Cook" <ji...@iname.com>
> > > One dumb question: For what do you _need_ the length? Velocity gives
> > > you an implicit iterator for arrays with #foreach()
> >
> > #if (a.length == 0)
>
> Hi,
>
> I guess you could do :
> #set ( $i = 0)
> #foreach( $foo in $bar )
>     #set($i = $i + 1)
> #end
> #if ($i == 0)
>     The array is empty
> #else
>     The array contains elements
> #end
> which is quite ugly, but works ...
>
>
> >
> > -jim
> >
> > --
>
>
>
> _________________________________________________________
> Do You Yahoo!?
> Get your free @yahoo.com address at http://mail.yahoo.com
>
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Accessing the length of an array

Posted by Stephane MOR <st...@yahoo.fr>.
----- Original Message ----- 
From: "James Cook" <ji...@iname.com>
> > One dumb question: For what do you _need_ the length? Velocity gives
> > you an implicit iterator for arrays with #foreach()
> 
> #if (a.length == 0)

Hi,

I guess you could do :
#set ( $i = 0)
#foreach( $foo in $bar )
    #set($i = $i + 1)
#end
#if ($i == 0) 
    The array is empty
#else
    The array contains elements
#end
which is quite ugly, but works ...


> 
> -jim
> 
> --



_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: Accessing the length of an array

Posted by James Cook <ji...@iname.com>.
> -----Original Message-----
> From: henning@forge.intermeta.de [mailto:henning@forge.intermeta.de]On

> One dumb question: For what do you _need_ the length? Velocity gives
> you an implicit iterator for arrays with #foreach()

#if (a.length == 0)

-jim

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Accessing the length of an array

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 11/20/01 10:33 AM, "Henning P. Schmiedehausen" <ma...@hometree.net>
wrote:

> "James Cook" <ji...@iname.com> writes:
> 
>>> This is an old argument :)
> 
>> I'm sure it was an old argument. I'm not trying to open old wounds, but
>> shouldn't the length of an array be accessible?
> 
> One dumb question: For what do you _need_ the length? Velocity gives
> you an implicit iterator for arrays with #foreach()
> 
> I'd rather see this with a velocity context tool along the suggestions here:
> 
> #set ($arraylen = $arraytool.Length($array))
> 
> with $array being your Array.
> 
> BTW: Geir: Why don't we have neither #do ... #while nor a 'real'
> #for() loop? I was asking me the other day how do I do a loop in
> Velocity that is not an Iterator over an object.

In the case of the while() and for() the idea is to prevent a template from
containing an infinte loop.  So we constrain looping to finite things.

You can do simple numbered for() loops with

#foreach($i in [0..10])
  $i
#end

#foreach($i in [0..$last])
  $i
#end

for example, w/o help from the Model or Controller.

Of course, there is always a way around this, like putting an infinte
Iterator() into the context, but that requires programmer [mis]
assistance...


> 
> Might be a nice exercise for my three hour train ride on friday to add
> this. ;-)
> 
> Regards
> Henning
> 
> 
> 
> 
> 

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
Be a giant.  Take giant steps.  Do giant things...


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Accessing the length of an array

Posted by "Henning P. Schmiedehausen" <ma...@hometree.net>.
"James Cook" <ji...@iname.com> writes:

>> This is an old argument :)

>I'm sure it was an old argument. I'm not trying to open old wounds, but
>shouldn't the length of an array be accessible?

One dumb question: For what do you _need_ the length? Velocity gives
you an implicit iterator for arrays with #foreach()

I'd rather see this with a velocity context tool along the suggestions here:

#set ($arraylen = $arraytool.Length($array))

with $array being your Array.

BTW: Geir: Why don't we have neither #do ... #while nor a 'real'
#for() loop? I was asking me the other day how do I do a loop in
Velocity that is not an Iterator over an object.

Might be a nice exercise for my three hour train ride on friday to add
this. ;-)

	Regards
		Henning






-- 
Dipl.-Inf. (Univ.) Henning P. Schmiedehausen       -- Geschaeftsfuehrer
INTERMETA - Gesellschaft fuer Mehrwertdienste mbH     hps@intermeta.de

Am Schwabachgrund 22  Fon.: 09131 / 50654-0   info@intermeta.de
D-91054 Buckenhof     Fax.: 09131 / 50654-20   

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Accessing the length of an array

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 11/20/01 10:34 AM, "Henning P. Schmiedehausen" <ma...@hometree.net>
wrote:

> "Geir Magnusson Jr." <ge...@optonline.net> writes:
> 
>> Can you suggest a syntax for such a thing?
> 
> $#array  :-)
> 

LOL.

There is this great language called 'Perl' you might be interested in...

:)

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
Be a giant.  Take giant steps.  Do giant things...


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Accessing the length of an array

Posted by "Henning P. Schmiedehausen" <ma...@hometree.net>.
"Geir Magnusson Jr." <ge...@optonline.net> writes:

>Can you suggest a syntax for such a thing?

$#array  :-)

	Regards
		Henning

-- 
Dipl.-Inf. (Univ.) Henning P. Schmiedehausen       -- Geschaeftsfuehrer
INTERMETA - Gesellschaft fuer Mehrwertdienste mbH     hps@intermeta.de

Am Schwabachgrund 22  Fon.: 09131 / 50654-0   info@intermeta.de
D-91054 Buckenhof     Fax.: 09131 / 50654-20   

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Accessing the length of an array

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 11/20/01 8:59 AM, "James Cook" <ji...@iname.com> wrote:

>> -----Original Message-----
>> From: Geir Magnusson Jr. [mailto:geirm@optonline.net]
> 
>> The reason is that in the spirit of the Java Bean spec, we wish
>> to push you
>> towards accessing via method rather than naked data in your objects - that
>> the philosophy that drives it.
> 
> There are many instances where Sun does not follow the JavaBean spec, and I
> suspect it is also why you allow method calls in Velocity and not only
> property accessors. Your decision has unfortunately caused a seemingly
> common thing like getting the length of an array somewhat challenging.

1) Yes - the JavaBean spec is too restrictive for Velocity, as we want to
allow a much more natural binding to Java objects.

2) It's utterly irrelevant in this case, as .length *is not* a member, so
you can't access it.

And it's not that hard if you put into the context an j.l.reflect.Array
object.

$set($length = $tool.getLength( $array ) )


> 
>> Yes, it's easy to do, but you get into interesting quandaries when you do
>> things like
>> 
>>   $foo.bar
>> 
>> And both
>> 
>> getBar() and the public member bar are available.  Which should velocity
>> call for you?
> 
> It should resolve according to the spec, which could have been written to
> try accessing a public method named "bar" after the normal flow fails to
> result in a match.

Which spec?

> 
> I'm not even advocating access to public variables. I would rather see that
> an array's length is accessible. As Atilla pointed out, it is not a public
> member variable, but rather an "artifical idiom". It is a special case in
> Java and perhaps it should be in Velocity.

I am not sure I agree :)

There are countless solutions to this problem.  I will agree that none are
as nice to the template designer as

  $foo.arrayLength


(or whatever...) but having a magic token in Velocity is something we have
avoided so far...

Can you suggest a syntax for such a thing?
 
>> This is an old argument :)
> 
> I'm sure it was an old argument. I'm not trying to open old wounds, but
> shouldn't the length of an array be accessible?

Because it's not accessible in reality anyway.  It is because your compiler
goes through special machinations on your behalf - but if you wrote
something in Java that was more dyanamic, that used reflection to work with
data and objects, you would be up the same creek, and would probably resort
to the same solution that you would do in the template.


-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
"He who throws mud only loses ground." - Fat Albert


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: Accessing the length of an array

Posted by James Cook <ji...@iname.com>.
> -----Original Message-----
> From: Geir Magnusson Jr. [mailto:geirm@optonline.net]

> The reason is that in the spirit of the Java Bean spec, we wish
> to push you
> towards accessing via method rather than naked data in your objects - that
> the philosophy that drives it.

There are many instances where Sun does not follow the JavaBean spec, and I
suspect it is also why you allow method calls in Velocity and not only
property accessors. Your decision has unfortunately caused a seemingly
common thing like getting the length of an array somewhat challenging.

> Yes, it's easy to do, but you get into interesting quandaries when you do
> things like
>
>   $foo.bar
>
> And both
>
> getBar() and the public member bar are available.  Which should velocity
> call for you?

It should resolve according to the spec, which could have been written to
try accessing a public method named "bar" after the normal flow fails to
result in a match.

I'm not even advocating access to public variables. I would rather see that
an array's length is accessible. As Atilla pointed out, it is not a public
member variable, but rather an "artifical idiom". It is a special case in
Java and perhaps it should be in Velocity.

> This is an old argument :)

I'm sure it was an old argument. I'm not trying to open old wounds, but
shouldn't the length of an array be accessible?

-jim


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Accessing the length of an array

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 11/20/01 8:36 AM, "James Cook" <ji...@iname.com> wrote:

>> -----Original Message-----
>> From: Geir Magnusson Jr. [mailto:geirm@optonline.net]
>> Sent: Tuesday, November 20, 2001 6:16 AM
> 
>> For an array (i.e.  String[] ), 'length' is a public member, not a method,
>> and Velocity doesn't allow access to public members.
>> 
>> (Here's a real place where this is a drag, I confess...)
>> 
> 
> Is there a technical reason for this restriction? Since it is as easy to
> reflect on a public variable as a public method, why choose not to?
> 

As Attila pointed out, it's not a public member, so the question is moot
here.

The reason is that in the spirit of the Java Bean spec, we wish to push you
towards accessing via method rather than naked data in your objects - that
the philosophy that drives it.

Yes, it's easy to do, but you get into interesting quandaries when you do
things like

  $foo.bar

And both

getBar() and the public member bar are available.  Which should velocity
call for you?

This is an old argument :)

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
Be a giant.  Take giant steps.  Do giant things...


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: Accessing the length of an array

Posted by James Cook <ji...@iname.com>.
> -----Original Message-----
> From: Geir Magnusson Jr. [mailto:geirm@optonline.net]
> Sent: Tuesday, November 20, 2001 6:16 AM

> For an array (i.e.  String[] ), 'length' is a public member, not a method,
> and Velocity doesn't allow access to public members.
>
> (Here's a real place where this is a drag, I confess...)
>

Is there a technical reason for this restriction? Since it is as easy to
reflect on a public variable as a public method, why choose not to?

-jim


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Accessing the length of an array

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 11/19/01 11:33 PM, "David Rees" <dr...@runt.ebetinc.com> wrote:

> On Mon, Nov 19, 2001 at 10:51:34PM -0500, James Cook wrote:
>> Is there any way to access the length of an array?
>>  
>> I have tried ${a.length} and many others, but was unsuccessful.
> 
> $a.length() should do the trick.
> 

Actually, I don't think it will.

For an array (i.e.  String[] ), 'length' is a public member, not a method,
and Velocity doesn't allow access to public members.

(Here's a real place where this is a drag, I confess...)

If you have lots of arrays that you are working with already, you could just
put a java.lang.reflect.Array in the context and use that as a tool to
manipulate your arrays.

Geir


-- 
Geir Magnusson Jr.                       geirm@optonline.net
System and Software Consulting
You're going to end up getting pissed at your software
anyway, so you might as well not pay for it. Try Open Source.



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Accessing the length of an array

Posted by David Rees <dr...@runt.ebetinc.com>.
On Mon, Nov 19, 2001 at 10:51:34PM -0500, James Cook wrote:
> Is there any way to access the length of an array?
>  
> I have tried ${a.length} and many others, but was unsuccessful.

$a.length() should do the trick.

-Dave

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>