Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^3: Binary search

by farang (Chaplain)
on Mar 29, 2013 at 17:41 UTC ( [id://1026180]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Binary search
in thread Binary search

This line will produce a non-integer value with an odd number of elements, which can't be an index of an array.

$mid=($low+$high)/2;
If you use the following at the top of your scripts and make the necessary corrections for it to compile, it will make your life easier and your code better.
use strict; use warnings;

Update: I see now that an array can be indexed with a non-integer value, effectively truncating the fractional part. I'm surprised that gives no warning.

use strict; use warnings; use feature 'say'; my @list = 0..9; say $list[2.5]; say $list[2.01]; say $list[2.99]; # all these print '2'
golux is correct of course about the sort, which is something that enabling 'strict' and 'warnings' would pick up.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-03-29 14:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found