Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

trying to print hashes with mixed results

by merrittr (Novice)
on Apr 09, 2021 at 01:56 UTC ( #11131035=perlquestion: print w/replies, xml ) Need Help??

merrittr has asked for the wisdom of the Perl Monks concerning the following question:

Here is my code and the results below, see how the hashes are printed not the values any ideas how I can get the data?
# read subplot ANOVA results my ($subplot_ERROR, $subplot_BLOCK) = &read_subplot_anova_stats($subpl +ot_anova_file); ###################################################################### +###### ##my $hash_ref = mysub(); while (my ($k, $v) = each(%$subplot_ERROR)) { print "error:$k = $v\n"; }
error:2019/Preston/W1 = HASH(0x3759530) error:2019/Preston/V2 = HASH(0x36378e0) error:2019/Preston/W3 = HASH(0x3640140) error:2019/Preston/V1 = HASH(0x3642ca8) error:2019/Preston/W2 = HASH(0x3637850) error:2019/Preston/STD = HASH(0x363fde0) error:2019/Preston/V3 = HASH(0x3764478) Block:2019/Preston/V1 = HASH(0x3637ec8) Block:2019/Preston/W2 = HASH(0x3643218) Block:2019/Preston/V3 = HASH(0x37565a8) Block:2019/Preston/STD = HASH(0x3642c90) Block:2019/Preston/W1 = HASH(0x3631b38) Block:2019/Preston/V2 = HASH(0x3626c80) Block:2019/Preston/W3 = HASH(0x375b610)

Replies are listed 'Best First'.
Re: trying to print hashes with mixed results
by parv (Vicar) on Apr 09, 2021 at 02:03 UTC

    You could use Data::Dumper ...

    use Data::Dumper; ... while( my ( $k, $v ) = each %hash ) { print Dumper( { qq[error:$k] , $v } ); }
      Sure that will work , but just from a learning standpoint how could I get that loop work and print the values?

        From your output you are; it's just the values are themselves hashrefs. You'd need to then iterate over those in turn. The simplest solution is going to be to use Data::Dumper (or YAML::XS or JSON::PP or . . .).

        The cake is a lie.
        The cake is a lie.
        The cake is a lie.

        > but just from a learning standpoint how could I get that loop work and print the values?

        you are looping over a hash-ref and $v is another hash-ref, again.

        so start another loop over $v.

        In the general case you'll need a recursive function which loops over hash-refs and array-refs.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

        Sure that will work , but just from a learning standpoint how could I get that loop work and print the values?

        See Hashes of Hashes in perldsc.

Re: trying to print hashes with mixed results
by NetWallah (Canon) on Apr 09, 2021 at 03:56 UTC
    uuse strict; use warnings; sub PrintHash{ my ($href, $indent_text, $level) = @_; $level ||= 0; while (my ($k, $v) = each(%$href)) { print +("\t" x $level), "${indent_text}$k = "; if (not ref $v){ #SCALAR print "$v\n"; }elsif (ref $v eq "HASH"){ print "\{\n"; PrintHash($v,"",$level+1); print +("\t" x $level),"}\n"; }elsif (ref $v eq "ARRAY"){ local $,=", "; print "\@(@$v)\n"; # Could do a better job by writing sub PrintArrayRef{} }else{ die "ERROR: Invalid type:",ref($v),"\n"; } } } PrintHash({Uno=>1, Dos=>2, Tres=>3, Cuatro=>[1,2,3,4], Cinco=>{otro=>9 +9,rojo=>'Red'}}, "Error codes:", 0);
    Output:
    > perl .\printhash.pl Error codes:Cuatro = @(1 2 3 4) Error codes:Uno = 1 Error codes:Dos = 2 Error codes:Tres = 3 Error codes:Cinco = { otro = 99 rojo = Red }
    If you have specific questions abut a specific line of code, go ahead and post it here.

                    "Avoid strange women and temporary variables."

      Holy crap ! that works great! thanks NetWallah
Re: trying to print hashes with mixed results
by BillKSmith (Monsignor) on Apr 09, 2021 at 02:51 UTC
    Your output tells us that your lexical variable $v is a reference to a hash. We do not know what that hash contains or how you want it printed. The module Data::Dumper is a great tool learn what it contains. With this knowledge, you should be able to dereference $v and loop through its keys the same way that you did $subplot_ERROR.
    Bill

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2022-06-25 17:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (83 votes). Check out past polls.

    Notices?