in reply to Re: printing hashes
in thread printing hashes

I'm surprised you didn't propose this variant of your earlier post today:

use List::Util 'reduce'; my %hash = ( foo => 1, bar => 2, baz => 3 ); my $count = 0; print +( reduce {$a . (++$count % 2 ? ' => ' : ', ') . $b} %hash ), $/; __END__ bar => 2, baz => 3, foo => 1
...or any other of the other join alternation schemes.

the lowliest monk

Replies are listed 'Best First'.
Re^3: printing hashes
by Roy Johnson (Monsignor) on May 06, 2005 at 02:53 UTC
    In that case, the question was "How do I get the output format I want?" This is a case of "Why doesn't Perl interpolate hashes in strings?" I only shamelessly plug my previous posts when it's justified.

    Of course, with the right syntax, Perl will interpolate anything in a string.

    my %hash = ( foo => 1, bar => 2, baz => 3 ); print "@{[%hash]}";
    But here's another reducing scheme that doesn't involve strenuous exercise or counting calories:
    print((reduce { $a . ($$...$$ ? ' => ' : ',') . $b } %hash), $/);
    The flip-flop doesn't maintain state between calls because in this case, it's not the same flip-flop on each call! Each entry into a subroutine gets a "different" flip-flop.

    Caution: Contents may have been coded under pressure.