You should always include "use strict" and "use warnings" when you are
writing code.  Take a look at the subryoutine and you will see:

$temp = $_[0] means that temp is now a reference to @array
$temp[0] is the first element of the array @temp, which was created when you
used the element

$$_[0][0] would then be the same as $$temp[0]

But to make sure that you can't make mistakes like this when dereferencing
an array, put braces around the reference:

${$_[0]}[0]
${$temp}[0]

Also, if you want to make it even easier to read, try this notation:

$temp->[0]

which is the same as:

${$temp}[0]

P.S.  The same works for hashes.  $temp->{key} accesses an element of the
hash referenced by $temp

-----Original Message-----
From: Paul Kraus [mailto:[EMAIL PROTECTED]]
Sent: Monday, January 06, 2003 1:30 PM
To: Perl
Subject: passing array ref to subroutine


I don't understand why the output of the two print statements inside the
subroutine is different. The one only prints the new line. 


#!/usr/bin/perl -w

@array=qw/paul david kraus/;
$arrayref=\@array;

print "$arrayref\n";
print "$$arrayref[0]\n";

&suby($arrayref);

sub suby {
  $temp=$_[0];
  print "$$_[0][0]\n";
  print "$temp[0]\n";
}

Paul Kraus
Network Administrator
PEL Supply Company
216.267.5775 Voice
216-267-6176 Fax
www.pelsupply.com


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to