Chris Garaffa wrote:

> Hello all,
> I'm working on a site for a t-shirt company, and doing the order process
> in Perl.
> I've got everything working, and working fine, but I was wondering about
> different ways to go about doing this:
>
> $the_order = new CGI;
> $the_sleeve_type = $the_order->param('sleeve_type');
> $the_previous_step = $the_order->param('step');
> %product_to_number = ();
> %number_to_description = ();
> $product_to_number{"short"} = "21";
> $product_to_number{"long"} = "20";
> $number_to_description{"21"} = "short sleeved t-shirt";
> $number_to_description{"20"} = "long sleeved t-shirt";
>
> (The above is simplified... There are many entries in product_to_number
> and number_to_description, as well as many more params passed to
> $the_order.
> Now, how do I go about defining multidimensional arrays, and then
> accessing them? What I have now is this:
>
> $product_number = $product_to_number{$the_sleeve_type};
> $product_description = $number_to_description{$product_number};
> print "[$product_number] [$product_description] ";
>
> but I'd like to be able to get rid of %product_description altogether
> and make calls to a multidimensional hash
> Any ideas?
>

Well if you're hardcoding the values as above you can write:

    #!/bin/perl -w
    use strict;

    my %products=();

    $products{short}= {
       number           => 21,
       description      => 'short sleeved t-shirt',
    };

    $products{long}= {
       number           => 20,
       description      => 'long sleeved t-shirt',
    };

    print "$products{short}{number} $products{short}{description}\n";
    print "$products{long}{number} $products{long}{description}\n";

If the values are coming from variables the syntax is essentially the same,
i.e.

    my $short='short';
    my $shortnum=21;
    my $shortdescription='short sleeved t-shirt';

    $products{$short}= {
       number           => $shortnum,
       description      => $shortdescription,
    };

There are, of course, other ways to write this. What's going on is that each
value in %products is a reference to an anonymous hash. Perl doesn't really
have hashes of hashes- it has hashes of references to hashes.

Tagore Smith



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

Reply via email to