Andrew Gaffney <[EMAIL PROTECTED]> wrote:
:
: I'm trying to write a subroutine that takes two scalars and two
: arrays as parameters. I've read that if you try to do this in a
: function, both arrays will get combined within '@_'.
Andrew,
- Stop using prototypes. You'll find it easier to write perl
programs without them.
- Read perlsub, perlref, and perlreftut.
- Install perl on your local computer.
- Experiment.
- Try calling your subroutines with strictures and warnings
turned on and see what happens. Your questions are /very/
basic. I realize this is a beginners list, but you'll
never get past beginner if you don't experiment.
: Now, how do I get those values in the subroutine?
:
: sub my_subroutine([EMAIL PROTECTED]@) {
: my ($scalar1, $scalar2, $arrayref1, $arrayref2) = @_;
: }
That's about it. Though I might suggest a style change.
- Use a variable names that describe the data, not it's
structure.
- Separate words in variables with underscores.
- Use comments and white space that aids the maintainer.
- Don't use prototypes.
sub sales_by_quarter {
my(
$first_quarter_name, # scalar
$second_quarter_name, # scalar
$first_quarter_data, # array reference
$second_quarter_data, # array reference
) = @_;
# ...
}
sub sales_comparison_by_quarter {
# These are references to arrays.
# Any changes you make *will* affect the original data.
# Think of them as read only.
my(
$Q1_data, # array reference
$Q2_data, # array reference
) = @_;
# Region names will default if not provided
my $Q1_region = shift || 'Region 1';
my $Q2_region = shift || 'Region 2';
# ...
}
: Another thing, how do you access an array through a reference?
: I know you access a hash through a reference by doing
: '$hashref->{hashkey}' instead of just '$hashref{hashkey}', but
: I've never done it with arrays (never needed to).
The arrow operator (->) is used to dereference references.
Read perlref and perl reftut.
$second_quarter_data->[ $month ]
: One more thing (I promise). Do I need to do anything special to
: pass arrays as references to the function, like this:
:
: my_subroutine $scalar1, $scalar2, [EMAIL PROTECTED], [EMAIL PROTECTED];
:
: or can I pass them without the '\'? Sorry for all the
: questions in one post, but at least they are all related :)
Yes, but you have already answered this at the beginning of
your message:
: I'm trying to write a subroutine that takes two scalars
: and two arrays as parameters. I've read that if you try
: to do this in a function, both arrays will get combined
: within '@_'.
HTH,
Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>