简体   繁体   English

不能使用字符串(“ a,b,c”)作为数组引用

[英]Can't use string (“a,b,c”) as an ARRAY ref

I am writing some code for an automated theorem prover. 我正在为自动定理证明者编写一些代码。 I wanted to implement an option for the user to pass a file on the cmd line and have it operate in batch mode. 我想为用户实现一个选项,以便在cmd行上传递文件并使它以批处理模式运行。

Here is the code to parse the file and fill the @clauses array. 这是解析文件并填充@clauses数组的代码。

# batch mode
if ($ARGV[0]) {

  my $filename = $ARGV[0];

  open(IN, "<", $filename);
  chomp(@clauses = <IN>);
  $conclusion2 = $clauses[@clauses - 1];

  # set sos as negated conclusion
  $SOS[0][0] = $conclusion2;

  # negate the negation to get the desired conclusion for later
  @conclusion = split(undef, $conclusion2);

  # look for a ~, remove it if you find it, add one if you don't
  for (my $i = 0 ; $i < @conclusion ; $i++) {

    # while you're at it.....
    # get rid of spaces and anything that isn't a ~ or letter
    $conclusion[$i] =~ s/( |[^A-Za-z~])//;
    if ($conclusion[$i] eq '~') {
      splice(@conclusion, $i, 1);
      $i--;
      $found = 1;
    }
  }

  if (!$found) {
    $conclusion = "~$conclusion2";
  }
  else {
    $conclusion = join(undef, @conclusion);
  }

  # now break up each line and make @clauses 2d
  for (my $a = 0 ; $a < @clauses ; $a++) {
    my $str = $clauses[$a];
    my @tmp = split(',', $str);
    for (my $b = 0; $b < @tmp; $b++) {
      $clauses[$a][$b] = $tmp[$b];       # ERROR HERE
    }
  }

  #       for(my $i=0; $i<@clauses;$i++)
  #       {
  #               print "$i";
  #               for(my $b=0; $b<=@{@clauses};$b++)
  #               {
  #                       print "$clauses[$a][$b]";
  #               }
  #               print "\n";
  #       }
}

I'm putting in more than I really need to, but the troublesome part is when I'm trying to break up the lines of the file by the commas and make the array two-dimensional. 我输入的内容超出了我的实际需要,但麻烦的部分是当我尝试用逗号分隔文件的行并将数组变成二维时。

At the line I have marked I get the error 在行上,我已标记为我收到错误

Can't use string ("a,b,c") as an ARRAY ref while "strict refs" in use

The input file is set up like this 输入文件是这样设置的

a,b,c
b,~c
~b
~a

This would be a proof to prove that a must be true 这将证明a必须为真

It's weird, because in the code for the interactive section, I do the exact same thing, almost verbatim and it works perfectly. 这很奇怪,因为在交互式部分的代码中,我几乎一字不漏地做着完全相同的事情,并且效果很好。

EDIT I'm certain that somehow, the error lies within this line 编辑我敢肯定,错误就在这行之内

$clauses[$a][$b] = $tmp[$b];

the error message is as follows: 错误消息如下:

can't use string ("a,b,c") as ARRAY ref while strict refs in use.

I don't see the need for any dereferencing on my part so what could the problem be? 我看不到我需要进行任何反引用,那么问题可能是什么呢?

You should really show as much of your program as possible, as it is hard to see the scope of the variables that you don't declare. 实际上,您应该尽可能多地显示程序,因为很难看到未声明的变量的范围。

I can assure you that, if you have use warnings in place as you should, then split undef will cause the warning 我可以向您保证,如果您已use warnings ,则split undef会导致警告

Use of uninitialized value in regexp compilation

The problem is that you have set 问题是您已经设定

$clauses[$a] = "a,b,c";
@tmp = ('a', 'b', 'c');

You then try to do 然后,您尝试做

$clauses[$a][$b] = $tmp[$b] for 0 .. 2

but $clauses[$a] is a string , not an array reference. 但是$clauses[$a]是一个字符串 ,而不是数组引用。 It is the same as writing 和写作一样

"a,b,c"[$b] = $tmp[$b] for 0 .. 2

which makes no sense. 这没有任何意义。 Hence the error message Can't use string ("a,b,c") as an ARRAY ref . 因此错误消息Can't use string ("a,b,c") as an ARRAY ref

A trivial fix would be to write 一个简单的解决方法是编写

$clauses[$a] = undef;

immediately after 之后

my $str= $clauses[$a]

so that the array element is now undefined, and Perl can autovivify an anonymous array here when the elements are copied. 这样,数组元素现在是未定义的,并且在复制元素时,Perl可以在此处自动保存匿名数组。

However your code could use a little more work, so here is a version that does what I think you want. 但是,您的代码可能需要做更多的工作,因此,这里有一个版本可以满足我的要求。 I have dumped the values of @clauses and $conclusion at the end to show their contents 我在末尾转储了@clauses$conclusion的值以显示其内容

use strict;
use warnings;

my $conclusion;
my $conclusion2;
my @SOS;

if (@ARGV) {

  my ($filename) = @ARGV;

  open my $in_fh, '<', $filename or die qq{Unable to open "$filename" for input: $!};
  chomp(my @clauses = <DATA>);
  close $in_fh;

  $conclusion2 = $clauses[-1];
  $SOS[0][0] = $conclusion2;

  $conclusion = $conclusion2;
  $conclusion =~ tr/A-Za-z~//cd;
  $conclusion = '~'.$conclusion unless $conclusion =~ tr/~//d;

  $_ = [ split /,/ ] for @clauses;

  print $conclusion, "\n";
  use Data::Dump;
  dd \@clauses;
}

output 输出

a
[["a", "b", "c"], ["b", "~c"], ["~b"], ["~a"]]

The problem is that $clauses[$a] is a string, not an arrayref, so $clauses[$a][$b] = ... doesn't really make sense. 问题在于$clauses[$a]是一个字符串,而不是arrayref,因此$clauses[$a][$b] = ...确实没有意义。 (It means "set element $b of the array referred to by $clauses[$a] to ... ", but $clauses[$a] doesn't refer to an array.) (这意味着“将$clauses[$a]引用的数组的元素$b设置为... ”,但是$clauses[$a]并不引用数组。)

To fix that, change this: 要解决此问题,请更改以下内容:

    my $str = $clauses[$a];
    my @tmp = split(',', $str);
    for (my $b = 0; $b < @tmp; $b++) {
      $clauses[$a][$b] = $tmp[$b]; # ERROR 
    }

to this: 对此:

    $clauses[$a] = [split /,/, $clauses[$a]];

so as to set $clauses[$a] to an arrayref with the contents you want. 以一套$clauses[$a] 你想要的内容的数组引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Perl - 不能使用字符串(...)作为数组引用 - Perl - can't use string (…) as an array ref 不能在“严格引用”时使用字符串作为 ARRAY 引用 - Can't use string as an ARRAY ref while "strict refs" 哈希数组; 不能在“严格引用”中使用字符串(“ 1”)作为阵列引用? - Array of hash; Can't use string (“1”) as an ARRAY ref while “strict refs”? perl:在使用“strict refs”时,不能将字符串用作ARRAY引用 - perl: Can't use string as an ARRAY ref while “strict refs” in use 不能将字符串(“ 1”)用作HASH引用,嵌入式哈希问题 - Can't use string (“1”) as a HASH ref, embedded hash issue JavaScript数组作为ref传递,但是不能将ref分配给新数组? - JavaScript array passed as ref but can't assign ref to a new array? 我可以保证非ref方法不会改变数组大小C# - Can I guarantee that a non-ref method doesn't change the array size C# 无法将字符串复制到C中的字符串数组 - Can't copy string to an array of strings in C 如何在数组的开关大小写上使用Scanner,以便大小写从字符串b中获取值 - How can I use Scanner on switch case for String of array so that case takes in value from String b 不能在 C 中将 sscanf() 用于 char 数组 - can't use sscanf() in C for char array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM