Re: Passing an array to a sub routine




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.

.



Relevant Pages

  • Array Problem
    ... When you define an array as Dim Arrayas Variant, ... routine that needs to recognized that if the array has not ... it needs to exit the sub. ... Dim ArrayPBas Variant ...
    (microsoft.public.excel.programming)
  • Speed versus memory
    ... My initial impulse was to use vector processes everywhere. ... an absurd amount of memory. ... Such a routine only has two input arrays ... and one output array but goes through ten intermediate arrays all of ...
    (comp.lang.fortran)
  • Passing an array to a sub routine
    ... The LoadDataFile routine works correctly, but the SaveDataFile ... routine only saves the last item in the array. ...
    (comp.lang.perl.misc)
  • RE: Can you fill an array from the contents of a single cell
    ... Sub ArmSelect1_Changeroutine. ... So then I moved it to the Private Sub CtlrSelect1_Click ... I can store the choices in a table in the proper array format. ... Dim CtlrArray() As String ' Defined as string array for ComboBox List ...
    (microsoft.public.excel.programming)
  • Re: CVF compiler question, program giving inconsistent results
    ... Run time error checking for "Array and string bounds" is turned on. ... There is no Monte Carlo stuff in this code. ... routine in a support library that generates a "random" ... Right after the LOGICAL INITS declaration there is ...
    (comp.lang.fortran)