So I was going merrily along putting things in a hash reference something like so:
my $href = { field1 => 'this', field2 => 'that', field3 => 'another', ...
when I realized that I wanted to map field3 to two different things, and that I actually had the keys and values backwards, but conceptually I still wanted the order to be the way I had it (because I was mapping a source to a target, and the source might have more than one target), so to save some typing (there were more than just 3 fields), I changed the hash ref to an array ref, and did a reverse on the array to build a new hash ref. I probably wouldn't do this in a loop; it was just a one time initialization thing. But what I really wanted was to be able to go:
my $href = { field1 <= 'this', field2 <= 'that', field3 <= 'another', field3 <= 'YetAnother', ...
Any thoughts on whether this is a good idea? Or should I just shut up and reverse my keys and values or the array ref? Duh, I just realized its a 'less than or equal to' operator, but still, could it be easily overloaded (or be some other operator entirely)?

Replies are listed 'Best First'.
Re (tilly) 1: The Oops operator.
by tilly (Archbishop) on Jul 26, 2001 at 23:27 UTC
    my $href = { reverse field1 => 'this', field2 => 'that', field3 => 'another', field3 => 'YetAnother', # etc };
      I like that :) But what I think I'll end up doing is using an array ref and passing that to another function, and reversing the array to a hash within the function, because I don't want the caller to be bothered with the reverse :)
Re: The Oops operator.
by dragonchild (Archbishop) on Jul 26, 2001 at 23:27 UTC
    Ummm... instead of whinging, you could've just written a script to do a split, reverse, and write out, treating it as a textfile. :)
      You mean like:
      #/usr/local/bin/perl -n -l ($value, $key) = /(\w+)\s*=>\s*'(.*?)'/; print "$key => '$value',";
      That was pretty much what I did not want to do, I wanted to keep the 'keys' and 'values' in the same order as I had them. BTW this was meant to be a passing thought in the realm of meditation, and I am sorry if I seemed to be whining. And although I wasn't really after wisdom, I ended up getting some from tilly anyway :)
      I think that would be a good function for a text editor to have.
        Most editors have the capability to take Perl functions. I know emacs and vi and crisp all can...