Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I get this array element? 1

Status
Not open for further replies.

cpjust

Programmer
Sep 23, 2003
2,132
US
Hi,
I've got a perl script that uses XMLRPC to talk to a Java app and it calls a Java function which returns an array of strings as Object[] but when I print out the first element I just see something like:
Code:
ARRAY(0x8b4d78)

I'm doing this:
Code:
my @keys;
my $key;
eval {
	@keys = $api->getAllExternalDataKeyNames();
	$key = $keys[0];
	print "*** key = " . $key;

The Java function is declared as:
Code:
public Object[] getAllExternalDataKeyNames( int unused ) throws XmlRpcException
 
if ARRAY(0x8b4d78) is coming out of $key that print @$key;

Travis

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
OK, now it prints 4, but I'm expecting some string like "key_name".
 
You'll probably want to take a look at the data structure with Data::Dumper. Something like:
Code:
use Data::Dumper;
print Dumper(@keys);
If you aren't sure how to get the value you want, paste some of the results from the Data::Dumper output (with any sensitive info changed/obscured.)
 
Nevermind, I got it.

Code:
@keys1 = @$key;
print "keys1[0] = " . $keys1[0];
 
Try something like this:

Code:
use Data::Dumper;
my @keys = $api->getAllExternalDataKeyNames();
print Dumper \@keys;

report back what gets output.


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Seems three of us were posting at almost the same time...

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Whenever I'm getting some kind of structure back like this I always use data dumper to print it out in the first instance, as Kevin has suggested - this allows you to see the structure clearly. After that it's easy to work out how to access the parts you need.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
The thing I don't understand is why Perl thinks it's a 2D array? I Java it's returning a 1D array of Strings (as Objects).
 
Is it a 2D array, or is it an array reference with 1 element, which is another array?

e.g.

Code:
$keys = [
   [
      'a',
      'b',
      'c',
   ],
];

where $$keys[0][0] = 'a', $$keys[0][1] = 'b', $$keys[0][2] = 'c', but $$keys[1][0] doesn't exist because $$keys only has one array element (which contains another array)?

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Yes, it was a 2D array with only one element on the outer array. Dumper reported something like:
Code:
VAR1 = [
   [
      'str1',
      'str2',
      'str3',
   ],
];
 
I don't define it as a "2D array" if the outer layer has only 1 element.

I have 2 theories:

1) Since you get the output of the function into a scalar ($keys), the function returns a reference to an array instead of an array. If you get the output into an array like @keys to begin with, it might work more like it would in Java (Perl has a wantarray function that modules use to see if you're calling their functions in array context or not).

2) Perl has a weird way of translating Java's Object[] into Perl code, thinking of Object[] as an array reference, and in this case that array reference contains the actual array of data you wanted from the function.

At any rate, you can just dereference the inner array and use it like normal:

Code:
my @keys = @{ $keys->[0] };

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top