in reply to Print multiple hashes of arrays at same time

use List::Util qw[ shuffle ];; %h1 = map{ ("key$_", "value$_" ) } 0 .. 9;; @selected = (shuffle keys %h1 )[ 0 .. 4 ];; @h2{ @selected } = @h1{ @selected };; printf "%-20s %-20s\n", $h1{ $_ }, $h2{ $_ } // '' for sort keys %h1;; value0 value0 value1 value1 value2 value2 value3 value4 value4 value5 value6 value6 value7 value8 value9

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
RIP an inspiration; A true Folk's Guy

Replies are listed 'Best First'.
Re^2: Print multiple hashes of arrays at same time
by tomdbs98 (Beadle) on Jul 19, 2010 at 19:17 UTC
    This looks awesome, but I can't get it to compile.
    syntax error at testcode/test.pl line 10, near "@h2{ "
    Too bad I don't have the knowledge base to debug it D:
      It works for me. I copy'n'pasted the 5 lines of Perl code, and I am able to reproduce BrowserUk's results without any errors.
      Your issue may be related to use strict;. @h2{ @selected } = @h1{ @selected }; uses hash slices to copy values from one hash to another. However, if you simply affix a my, perl will interpret that as a malformed array declaration. If this sounds like your issue, precede the line in question with the statement my %h2;.

        Yes, declaring  my %h2; fixed that problem. Now it seems that I can't run it because of perl v 5.8.8 XD

         Search pattern not terminated at testcode/test.pl line 13 (#1)
        Note that since Perl 5.9.0 a // can also be the defined-or construct, not just the empty search pattern. Therefore code writ +ten in Perl 5.9.0 or later that uses the // as the defined-or can be misparsed by pre-5.9.0 Perls as a non-terminated search pattern.
        It appears BrowserUK is using it in the 'defined-or' context, and updating perl isn't something I can readily do (at work), so I'm gonna have to drop this til I get home. Thanks though.