0

I am new to perl programming and I am trying to build a script using several subroutines on it. For a start I am trying to run a short mocke script to work out subroutines behaviour, but I don't get to understand the input.

Here is my code:

sub prueba{
  my (@array1, @array2)=@_;
  if (scalar(@array1)<scalar(@array2)) {
    print @array1,"\n";
  } elsif (scalar(@array1)>scalar(@array2)){
    print @array2,"\n";
  }
};

my @primero=(1,5,9);
my @segundo=(1,7,8,9,6,5,6,9);

prueba(@primero,@segundo);

I am passing two arrays and I want the subroutine to retrieve the answer according to those arrays, but when I run it I get no output, not even warning errors messages... I already tried using the refference to the array, but still not working:

sub prueba{
  my (@array1, @array2)=@_;
  if (scalar(@array1)<scalar(@array2)) {
    print @array1,"\n";
  } elsif (scalar(@array1)>scalar(@array2)){
    print @array2,"\n";
  }
};

my @primero=(1,5,9);
my @segundo=(1,7,8,9,6,5,6,9);

prueba(\@primero,\@segundo);

3 Answers 3

6

You can't pass arrays to subs (and they can't return them). You can only pass a number of scalars. What you are doing is equivalent to the following:

prueba(1,5,9,1,7,8,9,6,5,6,9);

All of the arguments end up in @array1. What we do is pass references to arrays.

prueba(\@primero,\@segundo);

But that also requires changing the sub. Without change, all of the arguments still end up in @array1. See perlreftut for a start on working with references. You can use

sub prueba{
  my ($array1, $array2)=@_;
  if (scalar(@$array1)<scalar(@$array2)) {
    print "@$array1\n";
  } elsif (scalar(@$array1)>scalar(@$array2)){
    print "@$array2\n";
  }
}

or just

sub prueba {
  my ($array1, $array2) = @_;
  if    (@$array1 < @$array2) { say "@$array1"; }
  elsif (@$array1 > @$array2) { say "@$array2"; }
}

< and > expect a number, so they already impose scalar context. And might as well use say, though that requires use feature qw( say ); (or something like use 5.014; which does the trick as well).

Sign up to request clarification or add additional context in comments.

1 Comment

I see you've unaccepted the answer. Is there an issue with this answer?
3

You can use prototypes to make it look like you're passing multiple arrays, and have perl turn them automatically into references:

sub prueba :prototype(\@\@) {
    my ($array1, $array2) = @_;
    if (@$array1 < @$array2) {
        print @$array1,"\n";
    } elsif (@$array1 > @$array2){
        print @$array2,"\n";
    }
}

my @primero=(1,5,9);
my @segundo=(1,7,8,9,6,5,6,9);
prueba(@primero, @segundo);

But read the documentation carefully to understand the cases where the subroutine can be called without enforcing the prototype.

Comments

0

Thanks all I just figured out what I wanted. I found that I can actually pass an array to perl, however maybe I am not explaning mysfel properly.The thing is to load the arrays as follow, inside the subroutine.

my @primero=@{$_[0]};
my @segundo=@{$_[1]};

This means we are using the reference. Ehen running the function, we must write the \ before each input:

prueba(\@primero,\@segundo);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.