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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

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


New User

Nov 5, 2015, 3:19 PM

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

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

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

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


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.


Chris Charley

Nov 5, 2015, 4:20 PM

Post #2 of 4 (1178 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.

New User

Nov 5, 2015, 5:44 PM

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

Had tried that.
When I try

print "@$variable\n";

I get an error

Not an ARRAY reference at ....


Nov 5, 2015, 9:12 PM

Post #4 of 4 (1165 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.

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

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


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

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