How to print hash recursively?

Discussion in 'Perl Misc' started by Peng Yu, Dec 30, 2009.

  1. Peng Yu

    Peng Yu Guest

    while ( ($k,$v) = each %hash ) {
    print "$k => $v\n";
    }

    The above code would not print the content of $v, if $v points to
    complex data structure such as arrays, hash of arrays, etc.

    I'm wondering if there is a convenient function that can print a hash
    table recursively into the elements.
     
    Peng Yu, Dec 30, 2009
    #1
    1. Advertising

  2. Peng Yu <> wrote:
    >while ( ($k,$v) = each %hash ) {
    > print "$k => $v\n";
    >}
    >
    >The above code would not print the content of $v, if $v points to
    >complex data structure such as arrays, hash of arrays, etc.
    >
    >I'm wondering if there is a convenient function that can print a hash
    >table recursively into the elements.


    1: it is trivial to write your own
    2: Did you check Data::Dumper?

    jue
     
    Jürgen Exner, Dec 30, 2009
    #2
    1. Advertising

  3. Peng Yu

    Peng Yu Guest

    On Dec 30, 3:24 pm, Tad McClellan <> wrote:
    > Peng Yu <> wrote:
    > > while ( ($k,$v) = each %hash ) {
    > >     print "$k => $v\n";
    > > }

    >
    > > The above code would not print the content of $v, if $v points to
    > > complex data structure such as arrays, hash of arrays, etc.

    >
    > > I'm wondering if there is a convenient function that can print a hash
    > > table recursively into the elements.

    >
    > I think you are looking for:
    >
    >    perldoc Data::Dumper


    If there are many keys in %hash, is there an option in Data::Dumper so
    that only the first a few key value pairs are printed?
     
    Peng Yu, Dec 30, 2009
    #3
  4. Peng Yu

    sreservoir Guest

    On 12/31/2009 6:02 AM, Peng Yu wrote:
    > On Dec 30, 3:24 pm, Tad McClellan<> wrote:
    >> Peng Yu<> wrote:
    >>> while ( ($k,$v) = each %hash ) {
    >>> print "$k => $v\n";
    >>> }

    >>
    >>> The above code would not print the content of $v, if $v points to
    >>> complex data structure such as arrays, hash of arrays, etc.

    >>
    >>> I'm wondering if there is a convenient function that can print a hash
    >>> table recursively into the elements.

    >>
    >> I think you are looking for:
    >>
    >> perldoc Data::Dumper

    >
    > If there are many keys in %hash, is there an option in Data::Dumper so
    > that only the first a few key value pairs are printed?


    there is no enumeration in a hash
     
    sreservoir, Dec 31, 2009
    #4
  5. Peng Yu

    C.DeRykus Guest

    On Dec 30, 2:02 pm, Peng Yu <> wrote:
    > On Dec 30, 3:24 pm, Tad McClellan <> wrote:
    >
    > ...
    >
    > > I think you are looking for:

    >
    > >    perldoc Data::Dumper

    >
    > If there are many keys in %hash, is there an option in Data::Dumper so
    > that only the first a few key value pairs are printed?


    I don't know but you could easily partition out a few:

    # tie my %hash, 'Tie::IxHash'; # to ensure ordering
    # tie my %few, 'Tie::IxHash'; #

    @few{(keys %hash)[0..4]}=(values %hash)[0..4]; # dump only 5
    print Dumper \%few;

    --
    Charles DeRykus
     
    C.DeRykus, Dec 31, 2009
    #5
  6. Peng Yu

    C.DeRykus Guest

    On Dec 31, 2:33 am, "C.DeRykus" <> wrote:
    > On Dec 30, 2:02 pm, Peng Yu <> wrote:
    >
    > > On Dec 30, 3:24 pm, Tad McClellan <> wrote:

    >
    > > ...

    >
    > > > I think you are looking for:

    >
    > > >    perldoc Data::Dumper

    >
    > > If there are many keys in %hash, is there an option in Data::Dumper so
    > > that only the first a few key value pairs are printed?

    >
    > I don't know but you could easily partition out a few:
    >
    >   # tie my %hash, 'Tie::IxHash';  # to ensure ordering
    >   # tie my %few, 'Tie::IxHash';   #
    >
    >   @few{(keys %hash)[0..4]}=(values %hash)[0..4]; # dump only 5
    >   print Dumper \%few;
    >


    For relatively simple hashes only though... more complexity
    will probably frustrate attempts at satisfactory partitioning.

    --
    Charles DeRykus
     
    C.DeRykus, Dec 31, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. call_me_anything
    Replies:
    4
    Views:
    469
    Pete Becker
    Sep 30, 2007
  2. lallous
    Replies:
    2
    Views:
    712
    TerryP
    Sep 30, 2009
  3. rp
    Replies:
    1
    Views:
    543
    red floyd
    Nov 10, 2011
  4. Srijayanth Sridhar
    Replies:
    19
    Views:
    629
    David A. Black
    Jul 2, 2008
  5. mike

    how to print hash within hash

    mike, Jan 1, 2004, in forum: Perl Misc
    Replies:
    5
    Views:
    87
    @(none)
    Jan 7, 2004
Loading...

Share This Page