Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > pass array reference

Reply
Thread Tools

pass array reference

 
 
bjlockie
Guest
Posts: n/a
 
      06-15-2012
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";
}
 
Reply With Quote
 
 
 
 
Rainer Weikusat
Guest
Posts: n/a
 
      06-15-2012
bjlockie <(E-Mail Removed)> 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";
}
 
Reply With Quote
 
 
 
 
Jim Gibson
Guest
Posts: n/a
 
      06-15-2012
In article <(E-Mail Removed)>,
bjlockie <(E-Mail Removed)> 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
 
Reply With Quote
 
Rainer Weikusat
Guest
Posts: n/a
 
      06-15-2012
Jim Gibson <(E-Mail Removed)> 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)]
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
difference between pass by address and pass by reference!! blufox C Programming 2 04-03-2006 02:53 PM
pass by reference vs pass by pointer LuB C++ 6 09-23-2005 06:19 AM
Pass by reference / pass by value Jerry Java 20 09-09-2005 06:08 PM
Pass by Pointer * or Pass by Reference &? Robert C++ 10 08-24-2005 04:15 PM
Pass-by-reference instead of pass-by-pointer = a bad idea? Mr A C++ 111 07-14-2005 03:04 AM



Advertisments