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');
You then try to do
$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
which makes no sense. Hence the error message 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.
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, "
";
use Data::Dump;
dd @clauses;
}
output
a
[["a", "b", "c"], ["b", "~c"], ["~b"], ["~a"]]