Binary Search in Perl

Published on 11 October 2025 (Updated: 11 October 2025)

Welcome to the Binary Search in Perl page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

#!/usr/bin/env perl
use strict;
use warnings;

sub handle_error {
    print "Usage: please provide a list of sorted integers (\"1, 4, 5, 11, 12\") and the integer to find (\"11\")\n";
    exit(0);
}

sub check {
    my ($s) = @_;
    
    # Trim leading and trailing spaces
    $s =~ s/^\s+//;
    $s =~ s/\s+$//;
    
    # Check if there are spaces in the middle
    if ($s =~ /\s/) {
        handle_error();
    }
    
    # Check if it's a valid integer
    if ($s !~ /^-?\d+$/) {
        handle_error();
    }
    
    return int($s);
}

sub convert {
    my ($s) = @_;
    
    if (length($s) == 0) {
        handle_error();
    }
    
    my @v;
    my @parts = split(',', $s);
    
    foreach my $part (@parts) {
        push @v, check($part);
    }
    
    return @v;
}

# Main program
if (@ARGV < 2) {
    handle_error();
}

my @v = convert($ARGV[0]);
my $num = check($ARGV[1]);

# Check if array is sorted
for (my $i = 0; $i < @v - 1; $i++) {
    if ($v[$i] > $v[$i + 1]) {
        handle_error();
    }
}

# Binary search
my $start = 0;
my $end = scalar(@v);
my $ans = "false";

while ($start < $end) {
    my $mid = int(($start + $end) / 2);
    
    if ($num < $v[$mid]) {
        $end = $mid;
    }
    elsif ($v[$mid] < $num) {
        $start = $mid + 1;
    }
    elsif ($v[$mid] == $num) {
        $ans = "true";
        last;
    }
}

print "$ans\n";

Binary Search in Perl was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.