On 3/3/11 Thu  Mar 3, 2011  3:11 PM, "Jim Green"
<student.northwest...@gmail.com> scribbled:

> On Mar 3, 5:44 pm, shawnhco...@gmail.com (Shawn H Corey) wrote:
>> On 11-03-03 05:40 PM, Jim Green wrote:
>> 
>>> But is there a easier way of
>>> doing this I might not be aware of?
>> 
>> Given your brief description, no.  The problem is that you can't output
>> the first datum without reading the last, because the last may be the
>> first thing that needs to be outputted.
> 
> Hello, let me generalize this problem,
> lets say I have a hash with 2 levels of keys,
> I want to convert this hash to another hash but with the 2 levels of
> keys reversed.. I hope there is a module or sth that can do it.

That is a simple problem that doesn't need a module (although one may
exist).

Untested:

my %oldhash = ( a => { b=>c, d=>e }, f => {g=>h, i=>j}, ... );
my %newhash;
for( my($key1,$val1) = each %oldhash ) {
  for( my($key2,$val2) = each %{$val1} ) {
    $newhash{$key2}->{$key1} = $val2;
  }
}



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to