CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Printing contents of referenced array

 



saumrh
New User

Nov 5, 2015, 3:19 PM

Post #1 of 4 (1046 views)
Printing contents of referenced array Can't Post

Hi,
I am new to Perl.. I am trying to understand a Perl script by inserting print statements to understand various variables in the program.

Code
   my $variable = subRoutineCall ($a, $b, $c); 
print "$variable\n";


When I try to print the above variable, I get an output

Code
 ARRAY(0x35fc9c9790).

As per my understanding the $variable contains a reference to resultant array that is returned by the subRoutine.

I looked online for code on how to dereference this variable to print the actual contents of the resultant array. I found using "\" operator we can dereference the array. However I am still getting the same output
I am not sure of the structure or contents of the array, hence want to print it out.

Am I misunderstanding something? Can someone please assist in printing the contents.

Thanks.


Chris Charley
User

Nov 5, 2015, 4:20 PM

Post #2 of 4 (1040 views)
Re: [saumrh] Printing contents of referenced array [In reply to] Can't Post

You would use "\" to create a reference, for example, to an array - \@myarray. This creates a reference to @myarrray.

To dereference your $variable, use print "@$variable\n";

I tried searching the docs but could not find where this is explained.


saumrh
New User

Nov 5, 2015, 5:44 PM

Post #3 of 4 (1034 views)
Re: [Chris Charley] Printing contents of referenced array [In reply to] Can't Post

Had tried that.
When I try

Code
print "@$variable\n";


I get an error


Code
Not an ARRAY reference at ....



BillKSmith
Veteran

Nov 5, 2015, 9:12 PM

Post #4 of 4 (1027 views)
Re: [saumrh] Printing contents of referenced array [In reply to] Can't Post

The results you are reporting seem to be impossible. Your first post shows that $variable is an array reference. Your second post shows that it is not.

I recommend that you use the module Data::Dumper to debug this problem. It will give meaningful output in either case. Replace your print with print Dumper. Post all of its output.


Code
use Data::Dumper; 
...
print Dumper(\$variable);


Run your script several times to verify that the result is repeatable. Tell us the result.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives