The initialization block of your for loop is the immediate culprit. Adjusting to something like this resolves the warning:
for(;$stringArray[$string_max_index];$x++)
Otherwise you're accessing a value, but doing... nothing with it? That's what the warning is for.
I spot a few other problems, though:
- Your for loops are... a little funny, I don't know how else to put that.
- Array length is usually easiest to read with the
scalar
keyword. - Adding members to an array is usually done with the
push
keyword.
Using the above in combination:
for(my $x = 0; $x < scalar @stringArray;$x++)
{
my $found = 0;
my $test = $stringArray[$x];
my $y = 0;
for (my $y = 0; !$found && $y < scalar @uniqueArray;$y++)
{
if($test eq $uniqueArray[$y])
{
$found=1;
}
}
unless ($found)
{
push @uniqueArray, $stringArray[$x];
}
}
If the above for loops don't look sensible to you, now is a good time to look up some tutorials.
This could be simplified with foreach
loops:
foreach my $letter (@stringArray) {
...
}
Or with grep
searches:
my $found = grep { $_ eq $letter } @uniqueArray;
But, in the particular case of counting unique values, it's often simplest to assign to a hash:
my %uniques;
$uniques{$_} = 1 for @stringArray;
my $num_uniques = scalar keys %uniques;
Combining all of that:
my @letters = split(//, $input); #split input into array of chars
my %uniques; #declare empty hash
$uniques{$_} = 1 for @letters; #set hash key for each char
my $num_letters = scalar @letters; #count entries in letter list
my $num_uniques = scalar keys %uniques; #count unique keys in letter hash
Exercise for the reader: adjust the above code so that it counts the number of times each character is used.