Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Perl-can';t使用字符串(…)作为数组引用_Arrays_String_Perl_File Io - Fatal编程技术网

Arrays Perl-can';t使用字符串(…)作为数组引用

Arrays Perl-can';t使用字符串(…)作为数组引用,arrays,string,perl,file-io,Arrays,String,Perl,File Io,我在练习Perl时遇到了来自codeeval.com的挑战,我遇到了一个意想不到的错误。目标是逐行遍历文件,其中每行都有一个字符串和一个字符,用逗号分隔,并找到该字符在字符串中最右边的位置。我得到了错误的答案,所以当我得到以下错误时,我修改了代码以只打印变量值: 在char_pos.pl第20行第1行使用“strict refs”时,不能将字符串(“Hello world”)用作数组引用。 我的代码如下。您可以在标题中看到文件中的示例。您还可以看到原始输出代码,它错误地只显示每个字符串中最右边的

我在练习Perl时遇到了来自codeeval.com的挑战,我遇到了一个意想不到的错误。目标是逐行遍历文件,其中每行都有一个字符串和一个字符,用逗号分隔,并找到该字符在字符串中最右边的位置。我得到了错误的答案,所以当我得到以下错误时,我修改了代码以只打印变量值:

在char_pos.pl第20行第1行使用“strict refs”时,不能将字符串(“Hello world”)用作数组引用。

我的代码如下。您可以在标题中看到文件中的示例。您还可以看到原始输出代码,它错误地只显示每个字符串中最右边的字符

#CodeEval challenge: https://www.codeeval.com/open_challenges/31/
#Call with $> char_pos.pl numbers
##Hello world, d
##Hola mundo, H
##Keyboard, b
##Connecticut, n

#!/usr/bin/perl
use strict;
use warnings;

my $path = $ARGV[0];

open FILE, $path or die $!;

my $len;

while(<FILE>)
{
    my @args = split(/,/,$_);
    $len = length($args[0]) - 1;
    print "$len\n";
    for(;$len >= 0; $len--)
    {
        last if $args[0][$len] == $args[1];
    }


    #if($len > -1)
    #{
    # print $len, "\n";   
    #}else
    #{
    # print "not found\n";   
    #}

}
#代码评估挑战:https://www.codeeval.com/open_challenges/31/
#使用$>char\u pos.pl号码呼叫
##你好,世界,d
##霍拉·蒙多,H
##键盘,b
##康涅狄格州
#!/usr/bin/perl
严格使用;
使用警告;
my$path=$ARGV[0];
打开文件,$path或die$!;
我的$len;
while()
{
my@args=split(/,/,$);
$len=长度($args[0])-1;
打印“$len\n”;
对于(;$len>=0;$len--)
{
如果$args[0][$len]==$args[1],则为最后一个;
}
#如果($len>-1)
#{
#打印$len,“\n”;
#}否则
#{
#打印“未找到”\n;
#}
}
编辑: 根据下面的答案,以下是我要使用的代码:

#!/usr/bin/perl
use strict;
use warnings;
use autodie;


open my $fh,"<",shift;

while(my $line = <$fh>)
{
    chomp $line;
    my @args = split(/,/,$line);
    my $index = rindex($args[0],$args[1]);

    print $index>-1 ? "$index\n" : "Not found\n";
}

close $fh;
#/usr/bin/perl
严格使用;
使用警告;
使用自动模具;

打开我的$fh,“perl中的字符串是一种基本类型,而不是可下标数组。您可以使用该函数从中获取单个字符(也只是字符串)或子字符串


还要注意的是,字符串比较是用
eq
完成的;
=
是数字比较。

看起来您需要了解一些Perl函数。Perl有许多用于字符串和标量的函数,不可能一下子就知道它们

然而,Perl有一个很好的函数,它完全满足您的需要。您给它一个字符串,一个子字符串(在本例中,是一个字符),它从字符串的右侧查找该子字符串的第一个位置(从左侧执行相同的操作)

因为您正在学习Perl,所以最好能获得一些关于现代Perl和标准编码实践的书籍。这样,您就可以了解更新的编码技术和标准编码实践

  • -为您提供更新的编程帮助
  • -旧标准
  • -标准编码实践
下面是一个示例程序:

#!/usr/bin/perl

use strict;
use warnings;
use autodie;
use feature qw(say);

open my $fh, "<", shift;

while ( my $line = <$fh> ) {
    chomp $line;
    my ($string, $char) = split /,/, $line, 2;
    if ( length $char != 1 or not defined $string ) {
        say qq(Invalid line "$line".);
        next;
    }
    my $location = rindex $string, $char;
    if ( $location != -1 ) {
        say qq(The right most "$char" is at position $location in "$string".);
    }
    else {
        say qq(The character "$char" wasn't found in line "$line".)";
}
close $fh;
!/usr/bin/perl
严格使用;
使用警告;
使用自动模具;
使用特征qw(例如);
打开我的$fh,“
while($i=){
($string,$char)=拆分(“,”,$i);
推送(@str,$string);}
@join=split(“,$”),打印“$join[-1]\n”,foreach(@str);
__资料__
你好,世界,d
霍拉·蒙多,H
键盘,b
康涅狄格州

检查
perldoc substr
而不是
$args[0][$len]
,如果要比较字符串,请使用
eq
;它是更惯用的Perl。当您检查文档时(注意需要是
perldoc-f substr
才能按名称查找函数
substr
),请查找
perldoc-f rindex
。您可能希望在
/,*/
上拆分,以便在尾随字母之前留出空间。提示:您可能更喜欢
my($word,$letter)=拆分(/,/,$)
@args
是一个很糟糕的名字。我倾向于避免在
拆分中使用
$\ucode><代码>拆分(/,/)也会做同样的事情。@ikegami-我喜欢这种拆分方法。我以后会记住这一点。谢谢这太棒了。谢谢你的详尽回答!我会确保查看这些资源。Perl不是我最喜欢的语言,但它有助于清晰、详细的解释。
while($i=<DATA>){
($string,$char)=split(",",$i);
push(@str,$string);}
@join=split("",$_), print "$join[-1]\n",foreach(@str);



__DATA__
Hello world, d
Hola mundo, H
Keyboard, b
Connecticut, n