Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Passing a file handle and two arrays to a function by reference.

by subr01 (Novice)
on May 26, 2015 at 13:28 UTC ( [id://1127831]=perlquestion: print w/replies, xml ) Need Help??

subr01 has asked for the wisdom of the Perl Monks concerning the following question:

How to pass a filename or a filehandle, two arrays to a function? I have done this:

open INPUT,"$old"; my (@arrayold,@arraynew); myfunc(*INPUT,\@arrayold,\@arraynew) sub myfunc { my ($file,@array1,@array2) = @_ ; my $j = 0; my $k = 0; while (my $line = <$file) { my($word1,$word2) = split(",",$line); $array[$j++] = $word1; $array[$k++] = $word2; } }

As you can guess, my function reads a file which has info like:

1,//http:abcd 2,//http:avcd

into two arrays one containing the number and the other containing the address. I am only getting blank arrays. Am I passing it wrong? Kindly help.

Replies are listed 'Best First'.
Re: Passing a file handle and two arrays to a function by reference.
by toolic (Bishop) on May 26, 2015 at 13:34 UTC
    You pass scalars to your sub. Use scalars in your sub:
    sub myfunc { my ($file, $aref1, $aref2) = @_; my @array1 = @{ $aref1 };
    Pass by Reference
Re: Passing a file handle and two arrays to a function by reference.
by Athanasius (Archbishop) on May 26, 2015 at 13:46 UTC

    Hello subr01,

    I am only getting blank arrays.

    Just a note to add to the answers already given: when assigning to an array, it slurps up all the elements remaining on the right-hand side of the assignment. So after:

    myfunc(*INPUT, \@arrayold, \@arraynew) ... sub myfunc { my ($file, @array1, @array2) = @_;

    $file contains the fileglob *INPUT, @array1 contains two elements (both references), \@arrayold and \@arraynew, and @array2 is indeed empty.

    Which is why, when assigning a list of elements to a list of variables, the only place it makes sense to include an array variable on the left-hand side is at the end.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Passing a file handle and two arrays to a function by reference.
by neilwatson (Priest) on May 26, 2015 at 13:32 UTC
    You are passing references to your arrays, thus your sub should be using $array1 and $array2.

    Neil Watson
    watson-wilson.ca

      ... and you will have to dereference the arrays to store stuff in them:

      $array1->[$j++] = ...
Re: Passing a file handle and two arrays to a function by reference.
by neilwatson (Priest) on May 26, 2015 at 13:58 UTC

    I like named params when a sub takes multiple args.

    mysub( FH => *INPUT, arr1 => \@arr1, arr2 => \@arr2 ); sub mysub { my %args = @_; my $first = $args{arr1}->[0]; }

    As a bonus you can also have default values

    sub named_params_with_defaults { my %args = ( one => 'default value', two => 'default value', @_, # Will override the above values. ); } named_params_with_defaults( one => "override arg 'one'" );

    Neil Watson
    watson-wilson.ca

Re: Passing a file handle and two arrays to a function by reference.
by BillKSmith (Monsignor) on May 26, 2015 at 17:43 UTC
    I recommend that you also use lexical scalars as filehandles (Refer: open). One of their biggest advantages is that they can be passed as simply as any other scalar.
    Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1127831]
Approved by toolic
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-19 19:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found