开发者

Perl regexp use of uninitialized pattern patch

开发者 https://www.devze.com 2023-03-16 15:41 出处:网络
My script loads some stuff from some files in some arrays, you enter a text from the keyboard, the script searches the relevant part of the text in those arrays, if it finds it, it does something, if

My script loads some stuff from some files in some arrays, you enter a text from the keyboard, the script searches the relevant part of the text in those arrays, if it finds it, it does something, if not, well, another thing, at least in theory.

I get the following errors:

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)/;

My problem, I think, is at $plm3 =~ /arr_(\w+.txt)/;. I used it so that I can store the name of an array in $1.

Here's my code:

#!/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....


First of all, it's always a good idea to use strict pragma -- unless you have a valid reason to avoid it --.

Second, I don't see $plm3 initialized anywhere in your code. You have probably forgot to initialize it.


I think you are assigning something to variable $1 on line 121


Apparently there are some copy/paste issues which negates my initial answer.

Other mistakes, great and small:

  • You don't use strict. (fatal flaw)
  • Your opendir is used once, then never closed.
  • You use global filehandles, instead of lexical (e.g. open my $fh, ...)
  • Using a complext loop + splice instead of grep (@files=grep /^arr/i, @files)
  • Using chomp($_) when chomp per default chomps the $_ variable
  • I don't even know what this line means:

        if(grep $stare  =~ m/$_/i, @{$1}) {
    

You seem to be using a pattern match, where $_ is the pattern (which in this case is.. what? Nothing? Anything?), whose return value is used as a grep pattern for an array reference, that may or may not be initialized. A very horrible statement. If it indeed works as intended, the readability is very low.

  • Redeclaring $/ seems like a frivolous thing to do in this context, but I can't really tell, as the script ends there.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号