It depends on what you are trying to do. If you just want to initilize some keys to the same values, that's one thing. If you want them to be aliases, where deleting one deletes all or modifying one, modifies all then that's another and probably best accomplished with tie.

I belive I have a snippet somewhere here that does the latter. I'll try and scrounge around for it and post it when i find it.
update
Here it is. May I ask what you are trying to do? I did this for combining apps that used a tied config hash so I wouldn't have to "touch" any of them.
update Noticed bug in DELETE. Fixed.
package Tie::Hash::KeyAlias; use Carp; sub TIEHASH { my $class = shift; my $self = {}; $self->{aliases} = {} ; $self->{keys} = {@_}; $self->{keystore} = []; $self->{keystoreindex} = 0; bless $self, $class; } sub STORE { my ($self,$key,$value) = @_; if (exists $self->{aliases}{$key} ){ my $realkey = $self->{aliases}{$key}; $self->{keys}{$realkey} = $value; }else{ $self->{keys}{$key} = $value; } } sub FETCH { my ($self,$key) = @_; if (exists $self->{aliases}{$key} ){ my $realkey = $self->{aliases}{$key}; $self->{keys}{$realkey} ; }else{ $self->{keys}{$key}; } } sub DELETE { my ($self,$key) = @_; if (exists $self->{aliases}{$key} ){ my $realkey = $self->{aliases}{$key}; foreach my $k (keys %{$self->{aliases}}){ delete $self->{aliases}{$k} if $self->{aliases}{$k} eq $re +alkey; } delete $self->{keys}{$realkey} ; }else{ foreach my $k (keys %{$self->{aliases}}){ delete $self->{aliases}{$k} if $self->{aliases}{$k} eq $ke +y; } } delete $self->{keys}{$key}; } sub EXISTS { my ($self,$key) = @_; if (exists $self->{aliases}{$key} ){ my $realkey = $self->{aliases}{$key}; exists $self->{keys}{$realkey} ; }else{ exists $self->{keys}{$key}; } } sub DEFINED { my ($self,$key) = @_; if (exists $self->{aliases}{$key} ){ my $realkey = $self->{aliases}{$key}; defined $self->{keys}{$realkey} ; }else{ defined $self->{keys}{$key}; } } sub CLEAR { my $self = shift; $self = {}; } sub FIRSTKEY { my $self = shift; $self->{keystore} = [ keys %{$self->{keys}}, keys %{$self->{aliase +s}} ]; return shift @{$self->{keystore}}; } sub NEXTKEY { my $self = shift; return shift @{$self->{keystore}}; } sub alias { my $self = shift; my %aliases = (@_); foreach my $k (keys %aliases){ if (exists $self->{keys}{ $aliases{$k} } ){ $self->{aliases}{$k} = $aliases{$k}; }elsif(exists $self->{aliases}{ $aliases{$k} } ){ my $realkey = $self->{aliases}{ $aliases{$k} }; $self->{aliases}{$k} = $realkey; }else{ croak "Can't ALIAS to a non-existant key! $k => $aliases{$ +k}" ; } } } # EXAMPLE USAGE package main; use Data::Dumper; my %hash; tie %hash, 'Tie::Hash::KeyAlias'; $hash{key1} = '10'; $hash{anotherkey} = 55; tied(%hash)->alias( key2=>'key1', key3=>'key2'); print Dumper(\%hash); $hash{key3} ++; # Modify key1,key2,key3 print Dumper(\%hash); delete $hash{key2}; # delete key1,key2,key3 print Dumper(\%hash); untie %hash;


-Lee

"To be civilized is to deny one's nature."

In reply to Re: multiple keys in hash by shotgunefx
in thread multiple keys in hash by PetaMem

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.