On Sat, 2002-03-02 at 11:16, John Crockett wrote:
> I'm sure this is such a simple thing, most of you-all are laughing at me
> right now.
> 
> main_program
> {
>     blah;
>     blah;
> 
>     %input_parm = ();
>     $input_parm{PRICE} = "42.00";
>     $input_parm{PRODUCT_ID} = "1306";
>     $input_parm{INVENTORY_LOC} = "107";
>     $input_parm{CUSTID} = "23489";
> 
>     my_subroutine(%input_parm);
> 
>     more irrelevant code;
>     .
>     .
>     .
> }
> 
> 
> sub my_subroutine(%)
> {
>     This is where I have trouble. I can't figure out the notation for
> reading the hash!
> }
> 
> 
> Thanks for your help.
> 
> John Crockett
> [EMAIL PROTECTED]
> 
> (Take out NoSpam to e-mail me)

Firstly, you must declare prototypes _before_ you use them or they will
have no effect.

Secondly, you should rarely, if ever, use prototypes.  They are a nasty
bit of magic that is poorly understood by many and not yet needed by
most.

Instead use one of the following two methods:

When a hash is evaluated in a list context it is transformed into a list
of alternating of keys and values.  The constructor for a hash takes a
list of (key, value) pairs.  So the code to pass a hash looks like this:

<example>
#!/usr/bin/perl

use strict;

my %hash = (
        key1 => 'value 1',
        key2 => 'value 2',
        key3 => 'value 3',
);

print_hash(%hash);

sub print_hash {
        my %hash = @_;

        foreach my $key (sort keys %hash) {
                print "$key => $hash{$key}\n";
        }
}
</example>

There are two problems with passing hashes this way: it takes time to
decompose and rebuild the hash and the hash eats all of the parameters. 
So people often use references like this:

<example>
#!/usr/bin/perl

use strict;

my %hash = (
        key1 => 'value 1',
        key2 => 'value 2',
        key3 => 'value 3',
);

print_hash(\%hash, '=>');

sub print_hash {
        my ($hash, $connector) = @_;

        foreach my $key (sort keys %$hash) {
                print "$key $connector $$hash{$key}\n";
        }
}
</example>

-- 
Today is Sweetmorn the 61st day of Chaos in the YOLD 3168


Missile Address: 33:48:3.521N  84:23:34.786W


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to