arrays - Perl 脚本 - : Useless use of array element in void context at letter_counter. pl lin 38 和 44

标签 arrays perl for-loop brackets

这是我的第一个 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/

相关文章:

perl - 这个 Perl 格式错误的原因是什么?

regex - 在 sed 和 perl 中替换单引号

regex - 找到一个作为模式给出的行词另一个行词,awk,perl,sed,grep?

python - 如何在 Python 中迭代以空格分隔的 ASCII 文件

android - 将字符串转换为 JSON 数组时出错

javascript - 迭代拼接多个数组

Javascript::this.value 在 for 循环内无法正常工作?

c++ - 区分静态数组,指针和结构的C++模板

arrays - for 循环的简洁数学符号

java - 在 Java 中使用 "+,-,|"绘制形状