arrays and the interpreter

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

arrays and the interpreter

Steele, Josh
arrays and the interpreter

I'm trying to initialize an array using something like this

Offset = new int[]{A,B,C}

where A, B, C have already been set in the interpreter instance. I seem to be able to eval the above statement fine, but I can't seem to find a way to get the array back out from the interpreter - get() only returns Objects, not arrays.  Also, when I try to get Offset[0], it returns null, even though I know A has been set to a value.

Is there something I'm missing?  Can beanshell not do this yet?

Thanks,
Josh

-----------------------------------------------------------------------
Josh Steele
Software Engineer
Science Applications Group, Space Department
[hidden email]

Reply | Threaded
Open this post in threaded view
|

RE: arrays and the interpreter

Daniel Leuck
arrays and the interpreter

Hi Josh,

 

(comments below prefixed with DAN>)

 

trying to initialize an array using something like this

Offset = new int[]{A,B,C}

where A, B, C have already been set in the interpreter instance. I seem to be able to eval the above statement fine, but I can't seem to find a way to get the array back out from the interpreter - get() only returns Objects, not arrays.

DAN> arrays are Objects.  Just cast the result to an array like so:
int[] offset = (int[])interp.get(“offset”);

Also, when I try to get Offset[0], it returns null, even though I know A has been set to a value.

DAN> primitive arrays cannot contain nulls so I expect the problem lies elsewhere.  If you send your code to the list we can take a look.

Is there something I'm missing?  Can beanshell not do this yet?

DAN> I don’t think this is a BeanShell issue, but send your code to the list and we will take a look.

Reply | Threaded
Open this post in threaded view
|

Re: arrays and the interpreter

patniemeyer
In reply to this post by Steele, Josh

On Jul 19, 2005, at 1:57 PM, Steele, Josh wrote:

I'm trying to initialize an array using something like this

Offset = new int[]{A,B,C}

where A, B, C have already been set in the interpreter instance. I seem to be able to eval the above statement fine, but I can't seem to find a way to get the array back out from the interpreter - get() only returns Objects, not arrays.  Also, when I try to get Offset[0], it returns null, even though I know A has been set to a value.

Is there something I'm missing?  Can bean

You should be able to do one of two things:

either get the whole array (which is itself an Object)

  int [] myArray = (int [])interpreter.get( Offset );

or eval() the index operation:

  Integer element0 = (Integer)interpreter.eval( "Offset[0]" );



Pat
Reply | Threaded
Open this post in threaded view
|

Re: arrays and the interpreter

Alexey Zinger
In reply to this post by Steele, Josh
You'll never get primitives out of the interpreter because everything is stored
and manipulated as objects.  When you deal with primitives in the interpreter,
BeanShell uses its own autoboxing to transparently wrap and unwrap them in
appropriate reference type wrapper objects (Integer, Boolean, etc).  If you
know you want a numeric type, just do ((Number)interpreter.get("A")).intValue()
or something to that effect.

Not sure about the other issues.

--- "Steele, Josh" <[hidden email]> wrote:

> I'm trying to initialize an array using something like this
>
> Offset = new int[]{A,B,C}
>
> where A, B, C have already been set in the interpreter instance. I seem to be
> able to eval the above statement fine, but I can't seem to find a way to get
> the array back out from the interpreter - get() only returns Objects, not
> arrays.  Also, when I try to get Offset[0], it returns null, even though I
> know A has been set to a value.
>
> Is there something I'm missing?  Can beanshell not do this yet?
>
> Thanks,
> Josh
>
> -----------------------------------------------------------------------
> Josh Steele
> Software Engineer
> Science Applications Group, Space Department
> [hidden email]
>
>


Alexey
2001 Honda CBR600F4i (CCS)
1992 Kawasaki EX500
http://bsheet.sourceforge.net


               
____________________________________________________
Start your day with Yahoo! - make it your home page
http://www.yahoo.com/r/hs 
 


-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|

RE: arrays and the interpreter

Steele, Josh
In reply to this post by Steele, Josh
Thanks all for the help - everything worked as intended.  The index I was using to get into the arrays was (unbeknownst to me at the time) 1-based instead of 0-based, so things weren't coming back as intended.  It all works now though - thanks!
 
Josh
-----Original Message-----
From: Patrick Niemeyer [mailto:[hidden email]]
Sent: Tuesday, July 19, 2005 11:31 PM
To: Steele, Josh
Cc: [hidden email]
Subject: Re: [Beanshell-users] arrays and the interpreter


On Jul 19, 2005, at 1:57 PM, Steele, Josh wrote:

I'm trying to initialize an array using something like this

Offset = new int[]{A,B,C}

where A, B, C have already been set in the interpreter instance. I seem to be able to eval the above statement fine, but I can't seem to find a way to get the array back out from the interpreter - get() only returns Objects, not arrays.  Also, when I try to get Offset[0], it returns null, even though I know A has been set to a value.

Is there something I'm missing?  Can bean

You should be able to do one of two things:

either get the whole array (which is itself an Object)

  int [] myArray = (int [])interpreter.get( Offset );

or eval() the index operation:

  Integer element0 = (Integer)interpreter.eval( "Offset[0]" );



Pat