Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing arrays instead of FILES to sub-routines

Status
Not open for further replies.

calabama

Programmer
Feb 19, 2001
180
0
0
US
Hi,
Using this as an example I was trying to understand how to create the code below using an @array rather than a FILE when calling a sub-routine. I am not clear if this is possible or if I am just going about this all wrong.

Thanks for any assistance.


Code:
numberoflines =  &CountCurrentRecords('ppc_members.dbf');

Code:
sub CountCurrentRecords {
$number = 1;
    my($database) = ($_[0]);
open(DATABASE, "$database");
    while ($line=<DATABASE>) {
        $number++;
    }
    $number = 0 if $number < 0;
    close(DATABASE);
    return $number;
    }

In the begining
Let us first assume that there was nothing to begin with.
 
I wonder if you meant...

my $numberoflines = @array;

As a critique of your original function, it could be better written as:

sub CountCurrentRecords {
my $number = 0;
my $database = shift;
open(DATABASE, &quot;$database&quot;);
while (<DATABASE>) {
$number++;
}
close(DATABASE);
return $number;
}

You don't need to do the test for zero at the end and using shift simplfies the passing of the single entry parameter list.

Barbie
Leader of Birmingham Perl Mongers
 
missbarbell,

Thanks so much for your help.

I am probably missing something here but where does the subroutine get called in the revised line and how is it passing the array instead of the filename like in the old line?

Cal


my $numberoflines = @array;

numberoflines = &CountCurrentRecords('ppc_members.dbf');?

Code:
sub CountCurrentRecords {
    my $number = 0;
    my $database = shift;
    open(DATABASE, &quot;$database&quot;);
    while (<DATABASE>) {
        $number++;
    }
    close(DATABASE);
    return $number;
}

In the begining
Let us first assume that there was nothing to begin with.
 
when using

my $numberoflines = @array;

it is not necessary to call the sub you use. To give a more complete view, this code should do what you want:

use strict;
use warnings;

open (DATABASE, &quot;$database&quot;);
my @db = <DATABASE>;
my $number = @db;


Assigning an array to a scalar (my $number = @db) returns the number of members in the scalar. Or in this case, the number of lines.
 
Hi,
I thought I would try this one more time. I am sorry for being so bad at communicating what I want to accomplish.

Below is an example of a call made to the sub CountCurrentRecords() and missbarbell's improved sub routine sub CountCurrentRecords().

The example shows the call using a FILE.

I was trying to understand how to create a sub and the call to the sub using an @array instead of a FILE. This question has nothing to do with the technical attributes of the sub routine itself.

------------------------------------------------
If I use this call
my $numberoflines = @array;
How does the sub know that @array is to be used as DATABASE in sub CountCurrentRecords() ?



Thanks for any assistance.

########## EXAMPLE CALL w/FILE ##############

my numberoflines = &CountCurrentRecords('ppc_members.dbf');
############### SUB ROUTINE ###############

Code:
sub CountCurrentRecords {
    my $number = 0;
    my $database = shift;
    open(DATABASE, &quot;$database&quot;);
    while (<DATABASE>) {
        $number++;
    }
    close(DATABASE);
    return $number;
}









In the begining
Let us first assume that there was nothing to begin with.
 
Still not quite sure what you are trying to do. The @array you refer to, does this contain the lines you want to count or is it a list of DATABASE files?

If you are just trying to see how to pass @array to the subroutine then both of the following will work:

$value = mysub(@array);
sub mysub {
my $num = @_;
return $num;
}

__OR__

$value = mysub(\@array);
sub mysub {
my $array = shift;
my $num = @$array;
return $num;
}

The first passes the contents of @array as a list, the second passes @array as a reference, which is then deferenced in the sub with @$array.

If this is the kind of thing you were trying to understand, also bear in mind that with the first, any other values you want to pass through to the sub might get lost, unless you take precautions. For example:

$value = mysub(@array,$anumber);
sub mysub {
my $max = pop @_; # takes the last parameter off the list
my $num = @_;
return $num;
}

The value returned by the sub will be one greater, but

$value = mysub($anumber,@array);
sub mysub {
my $max = shift; # takes the first parameter off the list
my $num = @_;
return $num;
}

HTH

Barbie
Leader of Birmingham Perl Mongers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top