in reply to Question about returning array from sub

Each element of the top level array is itself an arrayref, pointing to another array. You can't say print @array and get what you want in that case (print isn't that smart).

Can you show a toy sample code of how your are calling and catching the results?

However, if you use dd from Data::Dumper, or write your own array pretty printer, you'll get what you want.

-QM
--
Quantum Mechanics: The dreams stuff is made of

Replies are listed 'Best First'.
Re^2: Question about returning array from sub
by ojagan (Novice) on Jul 10, 2015 at 12:51 UTC
    Here is what I am doing in the main code -
    @rot_mat1=eul2mat($data_doc1[$i],$data_doc1[$i+1],$data_doc1[$i+2]); @rot_mat2=eul2mat($data_doc2[$i],$data_doc2[$i+1],$data_doc2[$i+2]);
    The $data_doc variables carry the angles to the sub. And I would like to return the array which is the rotation matrix. I instead end up returning the references.

      You are returning the array. The array hold references to other arrays, that have to be dereferenced later when you want to access them.

      Actually, you should probably return a reference to the main array. If you are just returning it to one caller, which uses it and is done, there's no issue. But it's good practice to pass references around rather than making multiple copies of data.

      Your caller will do something like my @bar = function() while &function() is also building up and returning its own array my @foo. Better for the function to return, and the caller to take, a reference to @foo: my $bar = function() and in &function: my @foo = (1,2,3); return \@foo;

      Remember: Ne dederis in spiritu molere illegitimi!