Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing array to subroutine Perl

Tags:

arrays

perl

I am currently learning Perl and I want to pass an array to subroutine and print its content. I found that reference to array should be passed, however I am missing something:

arrayPrint($a,$b,\@array);

sub arrayPrint{
    my ($a,$b,@array) = @_;

    for(my $i=0; $i<=$#array;$i++){
        print $file $a.$b.$array[i]."\n";
    }
}
like image 327
Dana Avatar asked Mar 02 '26 18:03

Dana


2 Answers

You can't pass arrays (or hashes) to subs. You can only pass scalars to subs. (And they can only return scalars.)

What we can do is pass a reference to an array (or hash). References are scalars. And in fact, this is what you are doing.

The problem is that you are assigning it to an array. An array that always contains exactly one scalar just makes things complicated for nothing. It should be assigned to a scalar. And it should be treated as a reference.

sub arrayPrint {
   my ( $x, $y, $array ) = @_;

   for my $e ( @$array ) {
      say $fh "$x$y$e";
   }
}

arrayPrint( $x, $y, \@array );

The other approach would be to pass each element of the array to the sub.

sub arrayPrint {
   my ( $x, $y, @array ) = @_;

   for my $e ( @array ) {
      say $fh "$x$y$e";
   }
}

arrayPrint( $x, $y, @array );

or

sub arrayPrint {
   my $x = shift;
   my $y = shift;

   for my $e ( @_ ) {
      say $fh "$x$y$e";
   }
}

arrayPrint( $x, $y, @array );
like image 167
ikegami Avatar answered Mar 05 '26 11:03

ikegami


Don't use the special variables $a and $b as lexicals, it can break sort.

Your code is sending an array reference to the subroutine, but populating an array with it. The @array inside the subroutine will have a single element: the array reference.

You can send the array directly, which will in effect send all its members as individual arguments (very bad if the array can be large).

Or, send the reference, but populate a scalar with it. You can't use $# directly with a reference, though, you need to dereference it.

my ($x, $y, $array_ref) = @_;
for my $i (0 .. $#{$array_ref}) {

It's more common to iterate over the elements rather than indices, though.

for my $e (@$array_ref) {
like image 27
choroba Avatar answered Mar 05 '26 12:03

choroba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!