in reply to Modifying order of a hash

Really appreciate all of this help. This site is always great to hit up if you run into trouble. Rather than starting a new thread, I figured I would just ask my similar question here: How would I transform this:
my %hash = ( '103496-1' => [{ 'CLVD' => '5678', 'COMP' => '1234', 'FD +' => '0010', 'Files' => [{'File' => 'text.txt', 'hash' => 'a538346ad3 +485'},{'File' => 'text2.txt', 'hash' => '237d97892376a'}] }] );
into this:
my %newhash = ( '103496-1' => [{ 1234 => {'CLVD' => '5678', 'FD' => '001 +0', 'Files' => [{'File' => 'text.txt', 'hash' => 'a538346ad3485'},{'F +ile' => 'text2.txt', 'hash' => '237d97892376a'}] }] );
Basically the only difference is that I don't want to remove the front value and simply shift 1234 outside of the original array. Any thoughts or suggestions?

Replies are listed 'Best First'.
Re^2: Modifying order of a hash
by jethro (Monsignor) on Sep 26, 2008 at 01:43 UTC
    A question: What are you trying to do? Learn about HoHs ? Or solve a real problem? Solve several problems? Convert old data to some new format?
      The main reason is that its a real problem and I need to change the format of the input to make it easier to read into another program. Basically I have no control of the original structure of the data so thats why I need to transform it into this.
Re^2: Modifying order of a hash
by karavelov (Monk) on Sep 26, 2008 at 17:22 UTC
    This will do the job:
    my %newhash = map { $_ => [ map { { delete $_->{COMP} => $_ } } @{$hash{$_}} ] } keys %hash;

    EDIT:

    Or may be :

    my %newhash = map { $_ => [{ map { delete $_->{COMP} => $_ } @{$hash{$_}} }] } keys %hash;

    depending on what you want as final result: your example is vague because you have only one element in the inner array reference

    If it was my program I will be looking for result like:

    { '103496-1' => { '1234' => { 'FD' => '0010', 'CLVD' => '5678', 'Files' => [ { 'hash' => 'a538346ad3485', 'File' => 'text.txt' }, { 'hash' => '237d97892376a', 'File' => 'text2.txt' } ] } } };

    because you do not need an arrayref with just one element - hash reference. In which case use the following:

    my %newhash = map { $_ => { map { delete $_->{COMP} => $_ } @{$hash{$_}} } } keys %hash;
    Best regards
      This works great and I appreciate all of your help. I'm not that familiar with the map function but it has worked for me well so far. I'm now trying to incorporate multiple hashes rather than above where I have just one and it doesn't seem to work. I've wrapped a foreach statement around the code but would that mess up the map function?