Binary Search examplerestated from Mastering Algorithms with Perl, O'Reilly, 1999,http://oreilly.com/catalog/9781565923980.1Binary SearchIn our program, each word is represented in Perl as a scalar, which can be an integer, a foating-point number, or (as in this case) a string of characters.The list of words is stored in a Perl array: an ordered list of scalars.Perl Notation: Scalars begin with a $ sign, Arrays begin with an @ sign. Hashes begin with a % sign.Recall that hashes (aka associative arrays) “map” one set of scalars (the “keys”) to other scalars (the “values”).2Binary Search3Binary Search4nextThe next command is like the continue statement in C; it starts the next iteration of the loop:1. LINE: while (<STDIN>) {2. next LINE if /^#/; # discard comments3. #...4. }Binary Search56Binary SearchTry it out% perl bsearch.pl binary /usr/dict/words% perl bsearch.pl binary /usr/share/dict/words # OS Xbinary occurs at position
View Full Document