我的脚本从一些数组中的一些文件中加载了一些东西,你从键盘上输入一个文本,脚本搜索这些数组中文本的相关部分,如果找到了,它会做一些事情,如果没有,好吧,至少在理论上是另一回事。
我得到以下错误:
Use of uninitialized value in pattern match (m//) at emo_full_dynamic.pl line 120, <STDIN> chunk 2.
Modification of a read-only value attempted at emo_full_dynamic.pl line 121, <STDIN> chunk 2.
line 120 = $plm3 =~ /arr_(\w+.txt)/;我认为,我的问题出在$plm3 =~ /arr_(\w+.txt)/;。我使用它是为了在$1中存储数组的名称。
下面是我的代码:
#!/usr/bin/perl
use warnings;
$idx = 0;
$oldsep = $/;
opendir(DIR, 'c:/downloads/text_files/arrs/');
@files = readdir(DIR);
while ($idx <= $#files )
{
$value = $files[$idx];
if ( $value !~ m/^arr/i)
{
splice @files, $idx, 1;
}
else
{
$idx++;
}
}
foreach $plm (@files)
{
if($plm =~ m/txt$/)
{
open(ARR, "C:/downloads/text_files/arrs/$plm") or die $!;
while(<ARR>)
{ {
chomp($_);
$plm =~ m/arr_(\w+).txt/;
push(@{$1}, $_);
}
close ARR;
}
}
$plm = 0;
$idx = 0;
$stare = <STDIN>;
chomp($stare);
while($stare)
{
foreach $plm2 (@files)
{
if($plm2 =~ m/txt$/)
{
$plm2 =~ m/arr_(\w+).txt/;
if(grep $stare =~ m/$_/i, @{$1})
{
$flag = 1;
}
else
{
$flag = 0;
}
}
}
if($flag == 1)
{
$/ = "%\n";
$plm3 =~ /arr_(\w+.txt)/;
open SUPARARE, "C:/downloads/text_files/replies/$1" or die $!;
etc etc....发布于 2011-06-29 15:50:47
首先,使用严格编译指示总是一个好主意--除非您有充分的理由避免使用它。
其次,我没有在你的代码中看到$plm3被初始化。您可能忘记了初始化它。
发布于 2011-06-29 15:36:28
我认为您正在为第121行上的变量$1赋值
发布于 2011-06-29 16:53:37
显然,有一些复制/粘贴问题否定了我最初的答案。
其他大大小小的错误:
你不能使用
open my $fh, ...)@files=grep /^arr/i, @files)$_ <>H117>时,使用全局文件句柄,而不是词法(例如,当chomp per default chomp吞噬splice变量< grep >H117chomp($_) >)时,使用全局文件句柄而不是词法(例如,当chomp per default chomp吞噬$_变量<>H117>时,使用全局文件句柄而不是grep文件句柄)。
如果(grep $stare =~ m/$_/i,@{$1}) {
您似乎正在使用模式匹配,其中$_是模式(在本例中是..什么?没什么?任何内容?),其返回值用作数组引用的grep模式,可以初始化也可以不初始化。一个非常可怕的声明。如果它真的像预期的那样工作,可读性就会很低。
在这种情况下,
$/似乎是一件轻率的事情,但我真的不能说,因为脚本到此为止。https://stackoverflow.com/questions/6517167
复制相似问题