Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have the following two data structures:
my @type = qw(INTEGER INTEGER32 OCTET_STRING OBJECT_IDENTIFIER IPADDRE +SS COUNTER COUNTER32 GAUGE GAUGE32 UNSIGNED32 TIMETICKS OPAQUE COUNTE +R64); my %hash=( 'INTEGER' => INTEGER, 'INTEGER32' => INTEGER32, 'OCTET_STRING' => OCTET_STRING, 'OBJECT_IDENTIFIER' => OBJECT_IDENTIFIER, 'IPADDRESS' => IPADDRESS, 'COUNTER' => COUNTER, 'COUNTER32' => COUNTER32, 'GAUGE' => GAUGE, 'GAUGE32' => GAUGE32, 'UNSIGNED32' => UNSIGNED32, 'TIMETICKS' => TIMETICKS, 'OPAQUE' => OPAQUE, 'COUNTER64' => COUNTER64 );
In order to use the constant value of the hash I need to reference the keys of the hash by the array index.

I.E. $type[0] = INTEGER => $hash{'$type[0]'}

I'm using this in conjunction with some Tk code I wrote

# Create the list of BrowseEntry boxes for the right frame for (1..5) { $type_e[$_] = $right_frame_bottom->BrowseEntry( -variable => \$type[$_], -listwidth => '45', -width => 20, -choices => [@type], )->pack(-side => 'top', -pady => '3'); }
These 5 entry boxes get populated with the array @type

Any ideas, or am I making this to complicated?

update (broquaint): escaped bare square brackets

Replies are listed 'Best First'.
(jeffa) Re: How can I reference a hash value with an array
by jeffa (Bishop) on Feb 04, 2003 at 15:16 UTC
    Wow. What are you trying to do again? :) Looks like you are trying to build a look-up table out of @type so that you can validate certain types. You can save yourself a lot of typing by using a hash slice to generate the lookup table, but the contents of your hash are very confusing. You have needlessly quoted the keys and left the values bare -- are the values constants? Ahhh, i see now that they indeed are. Try this instead:
    # if these are constants, then don't quote them my @type = ( INTEGER, INTEGER32, OCTET_STRING, OBJECT_IDENTIFIER, IPADDRESS, COUNTER, COUNTER32, GAUGE, GAUGE32, UNSIGNED32, TIMETICKS, OPAQUE, COUNTER64, ); # construct hash my %hash; @hash{@type} = @type; # now, let's validate something warn "$type[0] not INTEGER" unless $hash{$type[0]} == INTEGER;
    Hope this helps, or at least sheds some more light on the problem.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: How can I reference a hash value with an array
by dragonchild (Archbishop) on Feb 04, 2003 at 15:17 UTC
    Try $hash{$type[0]} without the single-quotes. The single-quotes are un-iterpolated. Double-quotes would be interpolated.

    Of course, I'm a little curious why you have a hash of key-value pairs that are identical to one another. But, that's your project, not mine. :-)

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: How can I reference a hash value with an array
by broquaint (Abbot) on Feb 04, 2003 at 15:33 UTC
    Ahem ...
    my %hash = { $_ => (\&{$_})->() } qw( INTEGER INTEGER32 OCTET_STRING OBJECT_IDENTIFIER IPADDRESS COUNTER COUNTER32 GAUGE GAUGE32 UNSIGNED32 TIMETICKS OPAQUE COUNTER64 );
    So now you'll have a hash where the keys points to their corresponding constant values.
    HTH

    _________
    broquaint

Re: How can I reference a hash value with an array
by Anonymous Monk on Feb 04, 2003 at 16:22 UTC

    Previous posts have explained how you can do what you seem to want to do ... BUT WHY?

    Why set up a hash where the keys (constant strings), point to constant values that are the same as the keys???

    The only things you can do with a hash, are create it, set its value and reference its value.

    Why use a constant, to select a value, that is a constant and the same constant as you used to select it?

    Why not just use the constant?

    Not only did you make it more complicated than it need be, you found an ingenious way of doing so:)

Re: How can I reference a hash value with an array
by hardburn (Abbot) on Feb 04, 2003 at 15:10 UTC

    I think you're talking about creating a Hash of Arrays. Take a look at perldsc.