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.
But don't prototypes make debugging a little easier by throwing warnings if you're not passing the correct types of arguments to the function?
- Read perlsub, perlref, and perlreftut.
I will.
- 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.
I'm not exactly a beginner. I've been using Perl for about 8 months. It's just that I've never written a subroutine where I need to pass array or hash *references*.
: 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.
I do in real code. The above is just sample code.
- 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 '@_'.
I don't understand how that explains how I can pass the array and hash *references* to my function. If I use prototypes, this is taken care of for me. If I don't use prototypes, do I need the '\' in front of arrays and hashes in the function call? What about anonymous hashes?
my_subroutine $scalar1, $scalar2, {param1 => 'value1', param2 => 'value2'}, $arrayref;
In the above, wouldn't the hash just get squashed into @_? Wouldn't my @_ end up as the following within the function?
[$scalar1, $scalar2, 'param1', 'value1', 'param2', 'value2', $arrayref]
-- Andrew Gaffney Network Administrator Skyline Aeronautics, LLC. 636-357-1548
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>