I am attempting to write a script that will take both a string and a single character from the command line, and then search the string for the single character for the number of occurrences. I have attempted to do this by making the string into an array and looping through each individual element of the array, but I get 0 every time I try to do this. Is converting the string to the array of single characters possible or should I try a new method?
use strict;
use warnings;
if ($ARGV[0] eq '' or $ARGV[1] eq '') {
print "Usage: pe06f.pl string char-to-find\n";
exit 1;
}
my $string = $ARGV[0];
my $searchChar = $ARGV[1];
if (length($searchChar) > 1) {
print "Second argument should be a single character\n";
exit 2;
}
my @stringArray = split /\./,$string;
my $count = 0;
$i = 0;
for ( $i=0; $i <= length($stringArray); $i++) {
if ( $stringArray[$i] eq $searchChar) {
print "found $b at position $i";
$count++;
}
}
print "found $count occurrences of $searchChar in $string\n";