#!/usr/bin/perl use strict; use warnings; # Obtain a binary string from the user or die trying print "Your binary input please => "; chomp(my $bin = ); die "'$bin' is not binary\n" if $bin =~ /[^01]/; # Binary numbers are easier to convert right to left # We will reverse the numbers to do this my @position = reverse split //, $bin; # Now the right most value is in the first slot of the array # And the left most value is in the last slot of the array my $tot = 0; # Normally when we loop over an array, we loop over its values # In this case, looping over its indices allows us to do powers of 2 # $#position is the last index in the array for my $idx (0 .. $#position) { $tot += $position[$idx] * 2 ** $idx; # had a typo here with $digit[$idx] } print "\n$tot\n";