Re: Passing an array to a sub routine
- From: "Brian McCauley" <nobull67@xxxxxxxxx>
- Date: 17 Jan 2006 10:14:47 -0800
Bill H wrote:
> Subject: Passing an array to a sub routine
Your question is Frequently Asked: How can I pass/return a {Function,
FileHandle, Array, Hash, Method, Regex}?
Please refrain from posting FAQs.
> I am using the following 2 subroutines to load an array from a file and
> save it. The LoadDataFile routine works correctly, but the SaveDataFile
> routine only saves the last item in the array. The file that is read
> from is straight text in the format of:
>
> ITEM1\tVALUE1
> ITEM2\tVALUE2
>
> etc. The \t is a tab.
>
> I call the LoadDataFile with this:
>
> %myarray = &LoadDataFile("this is the file name");
Why is that & in there? Do you know what it does? If not then remove
it.
It is very unlikely that you really want to replace the contents of an
existing variable %myarray with the return value of the function. It
is far more likely that this is where you want %myarray to come into
being.
In Perl we don't usually use the word "array" to refer to associative
array - we use the (pendantically less correct term) "hash".
my %myhash = LoadDataFile("this is the file name");
> and it loads the %myarray with the correct values.
>
> I call the SaveDataFile with this:
>
> &SaveDataFile("this is the filename",%myarray);
>
> But it only saves the last entry. Am I doing somethign wrong in the
> call or the SaveDataFile routine?
Yes.
>
> Here are the routines:
>
> sub LoadDataFile
> {
> my $filename = shift;
> my $temp = "";
> my @dbf = ();
> my %ldf_dbf = ();
Nasty case of premature declaration you've got there. Also the
initializations are redundant.
> open(LDF_LPFILE,$filename);
You forgot to make the file handle local.
In recent Perl a lexical filehandle would be neater.
You forgot to check for errors.
The 2-arg open is largely a legacy (mis-)feature. All new code should
use the 3-arg one (IMNSHO).
open( my $ldf_lpfile, '<',$filename) or die "$filename: $!";
> flock(LDF_LPFILE,2);
You forgot to check for errors.
> @LDF_LPFILE = <LDF_LPFILE>;
There is no reason to slurp. If you want to precess the data a line at
a time then read it a line at a time.
> close(LDF_LPFILE);
If you'd localized the handle there'd be no need to explicitly close()
except to check for errors.
> foreach $temp (@LDF_LPFILE)
> {
> chop $temp;
> @dbf = split(/\t/,$temp);
> $ldf_dbf{$dbf[0]} = $dbf[1];
Always use the most natural representation of things unless there is a
reason to so otherwise. $dbf[0] and $dbf[1] are natually two
independant scalars, not the elements of an array.
> }
> return (%ldf_dbf);
See FAQ.
> }
>
> sub SaveDataFile
> {
> my $filename = shift;
> my %this_dbf = shift;
There is a problem wilth this line that Perl would have told you about.
Did you perhaps "forget" to enable warnings?
If you change 'shift' to '@_' then I suspect your orginal code would
work - but it would still be far from optimal.
.
- References:
- Passing an array to a sub routine
- From: Bill H
- Passing an array to a sub routine
- Prev by Date: Re: binmode in 1-liner?
- Next by Date: Re: Passing an array to a sub routine
- Previous by thread: Re: Passing an array to a sub routine
- Next by thread: Re: Passing an array to a sub routine
- Index(es):
Relevant Pages
|