Jeff 'japhy' Pinyan wrote:
Gunnar Hjalmarsson said:
Jeff 'japhy' Pinyan wrote:

The general way is:

  # to add %w to %q
  @q{keys %w} = values %w;

If there are overlapping keys, %w's values will be used.

Why not just

%q = (%q, %w);

Benchmark it to see which is better.

The slice method seems to be faster.

    use Benchmark 'cmpthese';
    my %y = (three => 3, four => 4);
    cmpthese -5, {
        hashslice => sub {
            my %x = (one => 1, two => 2);
            @x{ keys %y } = values %y;
        },
        lists => sub {
            my %x = (one => 1, two => 2);
            %x = (%x, %y);
        },
    };

Result:
             Rate     lists hashslice
lists     21936/s        --      -40%
hashslice 36517/s       66%        --

Using %q = (%w, %q) allows you to keep %q's values, though.

And requires less typing. :)

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to