in reply to map and each?

Unless you wait for perl6, you're stuck. But really, what's so bad about:
$gah = join(', ', map { "$_ = $hash{$_}" } keys %hash); # or push @gah, "$k = $v" while (my ($k, $v) = each %hash); $gah = join(', ', @gah);
/s
Update: Here's what you want! ;)
$gah = join ', ', map { join ' = ', each %hash } 1..keys %hash;

Replies are listed 'Best First'.
Re: Re: map and each?
by L0rdPhi1 (Sexton) on May 27, 2002 at 02:27 UTC
    Because here's my real problem:

    sub genHash { my $hashn = shift; return "\%$hashn = (\n" . join(",\n", map { $key = $_; "'$key' => " . (ref $$hashn{$_} ? '{' . join(",\n", map { "'$_' => qq~" . safeTilde($$hashn{$key}->{$_}) . '~' } keys %{$$hashn{$key}}) . '}' : 'qq~' . safeTilde($$hashn{$key}) . '~' ) } keys %$hashn) . ");\n"; } sub safeTilde { my $code = shift; $code =~ s/\~/\\~/g; return $code; }
    then you would call it by genHash(\%somehash)

    though it does work, it's just is a little long and messy..

    Oh, by the way, it takes a hash and generates the perl to re-create it :)

    Edit kudra, 2002-05-27 s/pre/code

      Oh, by the way, it takes a hash and generates the perl to re-create it :)

      Have you tried Data::Dumper?

      It excels at what you're trying to do, and can stringify all sorts of Perl data structures in addition to hashes.

      use Data::Dumper; my $foo = Data::Dumper->Dump( [ \%ENV ], [qw( ENV )] );

          --k.


      Oh, by the way, it takes a hash and generates the perl to re-create it :)
      As long as the value doesn't contain dollar signs or at-signs or backslashes, since you used qq instead of q. Oh, and the key can't contain any single quotes.

      I agree with the other poster: don't reinvent Data::Dumper without understanding everything it does first.

      -- Randal L. Schwartz, Perl hacker

Re^2: map and each?
by Aristotle (Chancellor) on May 27, 2002 at 04:35 UTC
    ++ for creative use of the .. operator. :-)
    ____________

    Makeshifts last the longest.