How can I print the contents of a hash in Perl?

前端 未结 11 1428
借酒劲吻你
借酒劲吻你 2020-12-22 15:52

I keep printing my hash as # of buckets / # allocated. How do I print the contents of my hash?

Without using a while loop would be most preferable (for

相关标签:
11条回答
  • 2020-12-22 16:11

    Here how you can print without using Data::Dumper

    print "@{[%hash]}";
    
    0 讨论(0)
  • 2020-12-22 16:11

    The easiest way in my experiences is to just use Dumpvalue.

    use Dumpvalue;
    ...
    my %hash = { key => "value", foo => "bar" };
    my $dumper = new DumpValue();
    $dumper->dumpValue(\%hash);
    

    Works like a charm and you don't have to worry about formatting the hash, as it outputs it like the Perl debugger does (great for debugging). Plus, Dumpvalue is included with the stock set of Perl modules, so you don't have to mess with CPAN if you're behind some kind of draconian proxy (like I am at work).

    0 讨论(0)
  • 2020-12-22 16:12

    I append one space for every element of the hash to see it well:

    print map {$_ . " "} %h, "\n";
    
    0 讨论(0)
  • 2020-12-22 16:13

    My favorite: Smart::Comments

    use Smart::Comments;
    # ...
    
    ### %hash
    

    That's it.

    0 讨论(0)
  • 2020-12-22 16:14

    I really like to sort the keys in one liner code:

    print "$_ => $my_hash{$_}\n" for (sort keys %my_hash);
    
    0 讨论(0)
  • 2020-12-22 16:19

    For debugging purposes I will often use YAML.

    use strict;
    use warnings;
    
    use YAML;
    
    my %variable = ('abc' => 123, 'def' => [4,5,6]);
    
    print "# %variable\n", Dump \%variable;
    

    Results in:

    # %variable
    ---
    abc: 123
    def:
      - 4
      - 5
      - 6
    

    Other times I will use Data::Dump. You don't need to set as many variables to get it to output it in a nice format than you do for Data::Dumper.

    use Data::Dump = 'dump';
    
    print dump(\%variable), "\n";
    
    { abc => 123, def => [4, 5, 6] }
    

    More recently I have been using Data::Printer for debugging.

    use Data::Printer;
    p %variable;
    
    {
        abc   123,
        def   [
            [0] 4,
            [1] 5,
            [2] 6
        ]
    }
    

    ( Result can be much more colorful on a terminal )

    Unlike the other examples I have shown here, this one is designed explicitly to be for display purposes only. Which shows up more easily if you dump out the structure of a tied variable or that of an object.

    use strict;
    use warnings;
    
    use MTie::Hash;
    use Data::Printer;
    
    my $h = tie my %h, "Tie::StdHash";
    @h{'a'..'d'}='A'..'D';
    p %h;
    print "\n";
    p $h;
    
    {
        a   "A",
        b   "B",
        c   "C",
        d   "D"
    } (tied to Tie::StdHash)
    
    Tie::StdHash  {
        public methods (9) : CLEAR, DELETE, EXISTS, FETCH, FIRSTKEY, NEXTKEY, SCALAR, STORE, TIEHASH
        private methods (0)
        internals: {
            a   "A",
            b   "B",
            c   "C",
            d   "D"
        }
    }
    
    0 讨论(0)
提交回复
热议问题