develooper Front page | perl.perl6.users | Postings from March 2019

hash copy

Thread Next
Marcel Timmerman
March 24, 2019 11:11
hash copy
Message ID:

I was wandering if the following is an assignment or a binding (or 
binding of the entries);

my Hash $sleep-wait = { :s1(4.3), :s2(2.1), :s3(5.3), :s4(10.4), :s5(8.7),};

my Hash $sleep-left = $sleep-wait;

I noticed that in the following piece of code the $sleep-wait hash 
entries were set to 0
I had to explicitly do a copy to let it function correctly; 'my Hash 
$sleep-left = %(|$sleep-wait);'
Also cloning didn't work.

use v6;

my Hash $sleep-wait = {

   :s1(4.3), :s2(2.1), :s3(5.3), :s4(10.4), :s5(8.7),


my Hash $sleep-left = $sleep-wait;

loop {

   # get shortest sleep

   my $t = 1_000_000.0;

   my $s;

   for $sleep-left.keys -> $k {

     if $sleep-left{$k} < $t {

       $t = $sleep-left{$k};

       $s = $k;



   # set back to original time

   $sleep-left{$s} = $sleep-wait{$s};

   note "Reset entry $s to $sleep-left{$s} (from $sleep-wait{$s})";

   # adjust remaining entries

   for $sleep-left.keys -> $k {

     next if $s eq $k;

     $sleep-left{$k} -= $t;

     $sleep-left{$k} = $sleep-wait{$k} if $sleep-left{$k} <= 0;


   note "sleep for $t sec, entry $s";

   sleep $t;


perl version;

This is Rakudo version 2018.12-363-ga1c2e20d7 built on MoarVM version 
implementing Perl 6.d.


Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About