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

hash copy

Thread Next
From:
Marcel Timmerman
Date:
March 24, 2019 11:11
Subject:
hash copy
Message ID:
9158bdcb-d90f-c73d-3eb5-223da6cc17c2@gmail.com
Hi,

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 
2018.12-117-gdcafbc4c7
implementing Perl 6.d.


Regards,
Marcel

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About