Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Format a Dumper module output file.

by oxydeepu (Novice)
on Oct 03, 2012 at 12:17 UTC ( #997041=perlquestion: print w/ replies, xml ) Need Help??
oxydeepu has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I want to format the output from the dumper module, which is in this format given below

$VAR1 = {
'3' => {
'GID' => 18,
'AIG' => 161,
'TEA' => 224,
},
'2' => {
'GID' => 27,
'AIG' => 160,
'TEA' => 224,
},
'4' => {
'GID' => 17,
'AIG' => 135,
'TEA' => 214,
},
'1' => {
'GID' => 18,
'AIG' => 154,
'TEA' => 235,
}
};

In the file 1,2,3,4 are column numbers and values for corresponding 3 letter words. I want to format the file and get an out put as given below

AIG 154 160 161 135
GID 18 27 18 17
TEA 235 224 224 214

I am not able to get a logic to do so.
Please help. Thanks in advance.
Deepak

Comment on Format a Dumper module output file.
Replies are listed 'Best First'.
Re: Format a Dumper module output file.
by choroba (Canon) on Oct 03, 2012 at 12:32 UTC
    The comments should explain what's going on:
    my %subkeys; # The keys of %subkeys are the subkeys, i.e. GID, AIG, TEA in this cas +e. undef @subkeys{map keys %{ $VAR1->{$_} },keys %$VAR1}; # For each subkey, go through the hash and pick up the corresponding v +alue. for my $subkey (sort keys %subkeys) { print +($subkey, join "\t", q(), map $VAR1->{$_}{$subkey}, sort { $a <=> $b } keys %$VAR1), "\n"; }
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Format a Dumper module output file.
by Arunbear (Parson) on Oct 03, 2012 at 13:12 UTC
    The output of Data::Dumper can be turned back into a Perl variable using eval (or do if it's in a file), so
    #!/usr/bin/perl use strict; use warnings; my $file = shift or die 'no file given'; # parse output file my $var = do $file; my %words; foreach my $col (sort keys %$var) { my $hash = $var->{$col}; # collect values for each word while (my ($k, $v) = each %$hash) { push @{ $words{$k} }, $v; } } foreach my $word (sort keys %words) { printf "%s %s\n", $word, join(' ', @{ $words{$word} }); }
    Then
    % perl fmt.pl output.dump AIG 154 160 161 135 GID 18 27 18 17 TEA 235 224 224 214
    where output.dump is just
    $VAR1 = { '3' => { 'GID' => 18, 'AIG' => 161, 'TEA' => 224, }, '2' => { 'GID' => 27, 'AIG' => 160, 'TEA' => 224, }, '4' => { 'GID' => 17, 'AIG' => 135, 'TEA' => 214, }, '1' => { 'GID' => 18, 'AIG' => 154, 'TEA' => 235, } };

      Thank you all.
      The response was amazing.
      Thanks Arun,this what i needed.

      Best,
      Deepak

Re: Format a Dumper module output file.
by BrowserUk (Pope) on Oct 03, 2012 at 12:40 UTC

    #! perl -slw use strict; my $VAR1 = { '3' => { 'GID' => 18, 'AIG' => 161, 'TEA' => 224, }, '2' => { 'GID' => 27, 'AIG' => 160, 'TEA' => 224, }, '4' => { 'GID' => 17, 'AIG' => 135, 'TEA' => 214, }, '1' => { 'GID' => 18, 'AIG' => 154, 'TEA' => 235, } }; for my $f ( qw[AIG GID TEA] ) { print join ' ', $f, map{ $VAR1->{ $_ }{ $f } } sort{ $a <=> $b } k +eys %{$VAR1}; } __END__ C:\test>junk AIG 154 160 161 135 GID 18 27 18 17 TEA 235 224 224 214

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

Re: Format a Dumper module output file.
by nemesdani (Friar) on Oct 03, 2012 at 12:31 UTC
    1,2,3 and 4 are not files, but hashes - if I'm not trippin'. Assuming that, you can

  • create arrays for each hash value (GID, AIG, etc)
  • read each key in a hash, push the value into the array
  • repat this for all of the hashes



  • I'm too lazy to be proud of being impatient.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://997041]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (15)
As of 2015-07-28 18:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (258 votes), past polls