pass array reference

Discussion in 'Perl Misc' started by bjlockie, Jun 15, 2012.

  1. bjlockie

    bjlockie Guest

    I'm trying to pass a reference to an array so I can modify it in a function and see the changes outside the function.

    I tried:

    #!/bin/perl

    use strict;
    use warnings;

    my @myarray = ();
    &loadFile( '/etc/resolv.conf', \@myarray );

    print "Loaded $#myarray\n";

    sub loadFile
    {
    my $filename = $_[0];
    # input as a reference
    my (@inputArray) = @{$_[1]};

    open( FILE, "<$filename" ) or die "Can't open file ($filename): $!\n";
    @inputArray = <FILE>;
    close( FILE );

    print "Loaded $#inputArray from $filename\n";
    }
     
    bjlockie, Jun 15, 2012
    #1
    1. Advertising

  2. bjlockie <> writes:
    > #!/bin/perl
    >
    > use strict;
    > use warnings;
    >
    > my @myarray = ();
    > &loadFile( '/etc/resolv.conf', \@myarray );


    Don't use & to invoke a subroutine except if you actually
    intend to use its 'special powers': 1. Bypass prototype checking.
    2. 'Call forwarding': invoke the subroutine with the current @_ as
    'argument vector'.

    >
    > print "Loaded $#myarray\n";
    >
    > sub loadFile
    > {
    > my $filename = $_[0];
    > # input as a reference
    > my (@inputArray) = @{$_[1]};


    Here, you copy the contents of @myarray into @input array. What you
    actually need to do is to work with the passed reference, eg
    (untested)

    my $inputArray = $_[1];

    open( FILE, "<$filename" ) or die "Can't open file ($filename): $!\n";
    @$inputArray = <FILE>;
    close( FILE );

    print "Loaded $#$inputArray from $filename\n";
    }
     
    Rainer Weikusat, Jun 15, 2012
    #2
    1. Advertising

  3. bjlockie

    Jim Gibson Guest

    In article <>,
    bjlockie <> wrote:

    > I'm trying to pass a reference to an array so I can modify it in a function
    > and see the changes outside the function.
    >
    > I tried:
    >
    > #!/bin/perl
    >
    > use strict;
    > use warnings;
    >
    > my @myarray = ();
    > &loadFile( '/etc/resolv.conf', \@myarray );
    >
    > print "Loaded $#myarray\n";
    >
    > sub loadFile
    > {
    > my $filename = $_[0];
    > # input as a reference
    > my (@inputArray) = @{$_[1]};
    >
    > open( FILE, "<$filename" ) or die "Can't open file ($filename): $!\n";
    > @inputArray = <FILE>;
    > close( FILE );
    >
    > print "Loaded $#inputArray from $filename\n";
    > }


    You are making a local copy of the array and modifying that. This
    should work (untested):

    sub loadFile
    {
    my( $filename, $arrayref ) = @_;

    open( my $file, '<', $filename ) or die ...;
    @{$arrayref} = <$file>;
    close($file);

    print( scalar @{$arrayref}, " records read from $filename\n");
    }

    --
    Jim Gibson
     
    Jim Gibson, Jun 15, 2012
    #3
  4. Jim Gibson <> writes:

    [...]

    > sub loadFile
    > {
    > my( $filename, $arrayref ) = @_;
    >
    > open( my $file, '<', $filename ) or die ...;
    > @{$arrayref} = <$file>;


    The curlies are not necessary in this case, cf

    Anywhere you'd put an identifier (or chain of identifiers) as
    part of a variable or subroutine name, you can replace the
    identifier with a simple scalar variable containing a
    reference of the correct type:
    [perlref(1)]
     
    Rainer Weikusat, Jun 15, 2012
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jerry
    Replies:
    20
    Views:
    7,984
    Roedy Green
    Sep 9, 2005
  2. blufox
    Replies:
    2
    Views:
    575
  3. Mr A
    Replies:
    111
    Views:
    2,147
  4. Robert
    Replies:
    10
    Views:
    1,379
    E. Robert Tisdale
    Aug 24, 2005
  5. LuB
    Replies:
    6
    Views:
    464
    Mike Wahler
    Sep 23, 2005
Loading...

Share This Page