这是我的第一个 Perl 脚本
http://bpaste.net/show/171137/
#!/usr/bin/perl
#This program will take a user's input and then count how many letters there are. Whereupon it will count the number of unique letters before printing all the data
#back to the user.
use strict;
use warnings;
#======================================================================================================================
# This section is to collect and spit back the input to the user.
#======================================================================================================================
print "\n\nHello, please enter a word, a phrase, or a sentence. Press Enter when you are done.\n";
my $input = <>; #Collecting the input from the user.
chomp $input; #Chomping, or removing, the \n from the end of the input.
print "\nYou typed -:[$input]\n";
#======================================================================================================================
#This section will find how many unique characters there are.
#======================================================================================================================
my @uniqueArray;
my @stringArray = split(// , $input);
my $x = 0;
my $string_max_index = $#stringArray;
for($stringArray[$x];$stringArray[$string_max_index];$x++)
{
my $found = 0;
my $test = $stringArray[$x];
my $y = 0;
for($uniqueArray[$y];$uniqueArray[$#uniqueArray];$y++)
{
if($test eq $uniqueArray[$y])
{
$found=1;
}
}
if($found eq 1)
{
$uniqueArray[$#uniqueArray] = $stringArray[$x];
}
}
#======================================================================================================================
# This section will determine how many ascii characters are in the $input variable and output the results of this
# program.
#======================================================================================================================
my $numOfLet = 0;
while ( $input ne "" )
{
$numOfLet = $numOfLet + 1;
chop $input
}
print "Total Characters -: $numOfLet";
print "Total of Unique Characters -: $#uniqueArray \n\n\n";
exit;
我能够消除除这两个错误之外的所有错误,
Useless use of array element in void context at letter_counter.pl line 38
Useless use of array element in void context at letter_counter.pl line 44
令我困惑的是,这些行中没有任何内容,只有 for 循环的右括号,这让我相信问题是我在每个 for 循环中调用的元素。
最佳答案
for 循环的初始化 block 是罪魁祸首。调整为这样可以解决警告:
for(;$stringArray[$string_max_index];$x++)
否则,您正在访问一个值,但没有对它执行任何操作?这就是警告的目的。
不过,我还发现了一些其他问题:
- 你的 for 循环......有点有趣,我不知道还能怎么说。
- 数组长度通常最容易通过
scalar
keyword 读取。 . - 向数组添加成员通常使用
push
keyword 来完成.
结合使用以上内容:
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];
}
}
如果上面的 for 循环对您来说不合理,那么现在是查找一些教程的好时机。
这可以通过 foreach
loops 来简化:
foreach my $letter (@stringArray) {
...
}
或者使用 grep
searches :
my $found = grep { $_ eq $letter } @uniqueArray;
但是,在计算唯一值的特定情况下,分配给哈希值通常是最简单的:
my %uniques;
$uniques{$_} = 1 for @stringArray;
my $num_uniques = scalar keys %uniques;
结合所有这些:
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
给读者的练习:调整上面的代码,使其计算每个字符的使用次数。
关于arrays - Perl 脚本 - : Useless use of array element in void context at letter_counter. pl lin 38 和 44,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21297568/