Two ways to change multiple hash values at the same time

Somehow I ended up a binary assignment with the zip operator:

$giver<wallet given> Z+= (-$amount, $amount)

I was playing around with a simulation problem from Uri Wilensky (R’s gganimate is pretty cool, and pretty and cool).

That’s the same thing as writing out both sides:

$giver<wallet given> = $giver<wallet given> Z+ ( -$amount, $amount )

But, I also could have used a hyper here:

$giver<wallet given> >>+=<< ( -$amount, $amount )

I’ve longed wished for this sort of thing in Perl 5, and I think I’ve strained various unmaintainable kludges to get it. Even these Perl 6 expressions might be too mind bending for the work-a-day maintainer.

And, for what it’s worth, the results are pretty much what I expected despite the those Ph.D.s guessing incorrectly. But then, I also have a statistical thermodynamics book with arm’s reach of my keyboard. A closed system with a fixed number of particles and a fixed amount of energy where particles can transfer energy to other particles is a type of Spherical Cow. The system can exist in any of its microstates. Particle identity aside, some distributions of energy are more probable. The one that everyone might guess, that all particles will have the close to the same energy, just isn’t that likely. But, this isn’t a physics blog.

2 comments

  1. In Perl 5 you can do that. Just use hash slices. Here is an example:

    use Data::Dumper;
    my %a = 1..10;
    @a{1,3} = ( 666,666 );
    warn Dumper \%a;
    
    1. That’s not the same thing though. It’s not adding to values that are already there. You are merely replacing values.

Leave a Reply to brian d foy Cancel reply

Your email address will not be published. Required fields are marked *