On 09/07/2013 01:11 PM, eventual wrote:
Hi,
In the example below, how do I pass @pets and @numbers into the
subroutine so that
@animals = @pets  and
@digits = @numbers.
Thanks
my @pets = ('dogs' , 'cats' , 'horses');
my @numbers = (1..10);
&study (@pets , @numbers);
sub study {
      my (@animals, @digits) = (@_[0] , @_[1]);
      print "Animals = @animals\n";
      print "Digits = @digits\n";
}

Hi,

You should use an array reference and dereference them in the subroutine.

Also, do not use 'study', as it is defined already <http://perldoc.perl.org/functions/study.html>.


Here is the code:


use strict;
use warnings;

use Data::Dumper::Simple;

my @pets = ('dogs', 'cats', 'horses');
my @numbers = (1..10);

my_sub(\@pets, \@numbers); # array references are passed as arguments

sub my_sub {
  print Dumper(@_);        # just look how the @_ looks like

  my($animals, $digits) = @_;

  my @animals = @$animals; # dereference
  my @digits = @$digits;   # dereference

  print Dumper(@animals, @digits); # just look ...

  print "Animals = @animals\n";
  print "Digits = @digits\n";
}

Best regards,
Karol Bujacek


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to