Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by Bethany (Scribe)
on Jul 14, 2014 at 16:51 UTC ( #1093584=note: print w/replies, xml ) Need Help??


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

Using arithmetic division as a substitute for bit manipulation can create unexpected results. Since what you're doing here is shifting bits, why not treat $number like the binary it is? Use the shift right operator >> instead of dividing by two. http://perldoc.perl.org/perlop.html#Shift-Operators

(edited to add) Likewise, rather than using arithmetic modulo to to the equivalent of extracting the low bit, just use the bitwise and operator & to do a real bit mask operation. $number & 1 always gives the low bit of an integer $number, $number & 2 the next least significant bit, and so forth.

If personally coding a solution for that specific chore isn't important, see the concise, ready-to-use solutions in this question and answer. http://www.perlmonks.org/?node_id=55320 I've found that nineteen times out of twenty, consulting Perl Monks Super Search and Google (which often sends me to Perl Monks) brings up a solution ready to drop in as is or use with a little adaptation.

Hope this helps!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2022-12-03 09:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?