不能使用字符串(" A,B,C")作为数组引用 [英] Can't use string ("a,b,c") as an ARRAY ref

查看:208
本文介绍了不能使用字符串(" A,B,C")作为数组引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一些$ C $下一个自动定理证明。我想实现对用户提供一个选项,以通过对CMD线的文件,并把它在批处理模式下运行。

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.

下面是code解析文件,并填写 @clauses 阵列。

Here is the code to parse the file and fill the @clauses array.

# 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

这将是一个证明,证明 A 必须是真实的

This would be a proof to prove that a must be true

这很奇怪,因为在$ C $下的互动部分,我做同样的事情,几乎是逐字记录和它完美的作品。

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 of uninitialized value in regexp compilation

现在的问题是,你已设置

The problem is that you have set

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

您再尝试做

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

$子句[$一] 字符串的,而不是一个数组引用。它是相同的书写

but $clauses[$a] is a string, not an array reference. It is the same as writing

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

这是没有意义的。因此,错误信息不能使用字符串(A,B,C)作为数组引用

一个小小的更正是写

$clauses[$a] = undef;

立即的

my $str= $clauses[$a]

这样的数组元素现在是不确定的,和Perl可以的 autovivify 的这里匿名阵列时,元素被复制。

so that the array element is now undefined, and Perl can autovivify an anonymous array here when the elements are copied.

不过你的code可以使用更多一点的工作,所以这里是一个版本,做什么,我想你想要的。我有倾倒的值 @clauses $结论在最后展示自己的内容

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

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;
}

输出

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

这篇关于不能使用字符串(&QUOT; A,B,C&QUOT;)作为数组引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆