Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: I need help with the error "Modification of non-creatable array value attempted"

by davido (Cardinal)
on Jul 14, 2014 at 16:36 UTC ( #1093580=note: print w/replies, xml ) Need Help??


in reply to I need help with the error "Modification of non-creatable array value attempted"

Could you provide input that produces the error message? I haven't found the winning combination with the code you posted.

The easiest debugging step here would be to just put a "warn $position;" immediately before line 8 to verify that it is what you think it is.

The next step will be to make your code strict/warnings compliant, which it currently isn't. That could turn up a typo somewhere in a variable name of the code you're running.


Dave

  • Comment on Re: I need help with the error "Modification of non-creatable array value attempted"
  • Download Code

Replies are listed 'Best First'.
Re^2: I need help with the error "Modification of non-creatable array value attempted"
by Hramyzn (Novice) on Jul 14, 2014 at 16:45 UTC

    Thanks for the reply but I've already found the error. I've updated the code to what it currently is, EXCEPT that I've left the $number/= 2; line (which made the error appear). It seems that it tried to assign a $number % 2 to an element of an array, but it couldn't since $number wasn't guaranteed to be a whole number (and I guess that makes % fail), I've changed the $number/= 2; to $number = int($number/ 2) and now it works perfectly :)

      $number wasn't guaranteed to be a whole number (and I guess that makes % fail)

      No, Perl is OK with that:

      3:03 >perl -MData::Dump -wE "my @array; $array[5.35968 % 2] = 42; dd +\@array;" [undef, 42] 3:03 >

      But the original loop almost certainly wasn’t doing what you thought it was. With an initial value of 192:

      3:03 >perl -wE "my $iterations = 0; my $number = 192; while ($number +!= 0) { $number /= 2; ++$iterations; } say qq[$iterations iterations] +;" 1083 iterations 3:06 >

      The loop terminates when $number is zero, but (on my Perl, which is 64-bit) this test doesn’t succeed until division by two has reduced $number all the way down to half of 4.94065645841247 × 10-324 — which is a very small number!

      Hope that helps,

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1093580]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2022-12-03 09:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?