When you pass an array to a subroutine in Perl, it takes the arguments and
passes a list, I _think_ even if you use that format.  It would be much
better, IMHO to use references.  Something like:

###  SCRIPT  #########################################

use strict;    #Get in the habit of using these now.
use warnings;  #It will be a lot harder to go back later.

my $scalar = "zero";     #Don't name a variable $a
my @array1 = qw(one two three);  
my @array2 = qw(four five six);

print @array1;
print @array2;
subroutine($scalar,\@array1,\@array2); #passing a reference to each array

sub subroutine{
  my($a,$ref1,$ref2) = @_;    #@_ contains the arguments
  print @{$ref1};     #dereference the variable
  print @{$ref2};     #dereference the variable
}

###  END  ############################################

A few notes:  
1)It looks like you're used to programming in C.  Try not to let that
influence you TOO heavily.  Perl was made to look familiar, but is a
different animal(a Camel, apparently).  
2)Try not to use variables named '$a', '$b', or a number like '$1'.  These
are special.
3)'use strict' and 'use warnings' are your friends, although they may be a
little annoying at first.
4)Get used to declaring variables with my().  This will restrict their scope
so that you don't end up with a bunch of scalars that you have to explicitly
destroy if you want to free up memory for other variables.
5)Get the camel book if you don't have it.
6)Read as many of the man pages (perldoc perldoc) as you can stand(I know
it's like reading the encyclopedia, but the info's in there)
7)Feel free to post to this list with any other questions.  It's a great
source of information and people are for the most part very helpful.

-----Original Message-----
From: Shaun Bramley [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, September 24, 2002 5:57 PM
To: [EMAIL PROTECTED]
Subject: Array not being passed into a subroutine


Hello all,

I currently currently passing a subroutine one scalar value and two arrays.
Both array's work before and after the function call.  However only one of
hte arrays works within the subroutine.

example code:

print @array1;  //this works
print @array2;  //this works
subroutine($a, @array1, @array2);


sub subroutine($a, @array1, @array2)
{
print @array1; //this works
print @array2;  //this does not work
}

It is always the same array that is not being passed.  Even if I switch
@array1 and @array2 I still will only get one useable array within the
subroutine.

Thanks for the help

Shaun

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

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

Reply via email to