The use of the 'eq' operator, or the '=~', will force the lvalue into scalar context. Your array, as a scalar, is interpretted to be the length of the array at the time of reference. You are comparing "1" to ".".
You likely intend one of the following:
@{$Circ->[$i][$j]} == 1 && $Circ->[$i][$j][0] eq '.'
or,
"@{$Circ->[$i][$j]}" eq '.'
Note, in the latter case, that the array is being interpolated within a string. This code is equivalent to:
join($,, @{$Circ->[$i][$j]}) eq '.'
For simplicitly, I would choose to use a temporary variable:
my $cell = $Circ->[$i][$j];
... @$cell == 0 && $cell->[0] eq '.' ...
Cheers,
mark | [reply] [d/l] [select] |
Update1:
Come back with some code (only breifly tested):
$a = [[[1,2],[2,3]], [[3,4],[4,5]], [[4,5],[5,6]]];
$b = [[[1,2],[2,3]], [[3,4],[4,5]], [[4,5],[5,6]]];
print is_array_equal($a, $b);
sub is_array_equal {
my ($array1, $array2) = @_;
if ($#array1 != $#array2) {
return 0;
} else {
print "here\n";
for (my $i = 0; $i <= $#{$array1}; $i ++) {
print "there\n";
if ((ref($array1->[$i]) eq "ARRAY") && (ref($array2->[$i])
+ eq "ARRAY")) {
if (!is_array_equal($array1->[$i], $array2->[$i])) {
return 0;
}
} elsif (!ref($array1->[$i]) && !ref($array2->[$i])) {
print "compare $array1->[$i] and $array2->[$i]\n";
if ($array1->[$i] != $array2->[$i]) {
return 0;
}
} else {
return 0;
}
}
}
return 1;
}
Original:
Looks like you are comparing three dimentional arrays.
What you can do is to write a function that calls itself recursively:
- First it compares number of elements, if not equal, then the two arrays are not equal, so return false;
- If number of elements are the same, then iterate thru all elements, for each pair:
- If both elements are atomic, simply compare their values
- If one is atomic , one is collection, no need to continue, return false
- If both are collection, call this func recursively)
This would not only work for your three dimentional array, but array at any depth. | [reply] [d/l] |
You are trying to roll your own format and parser for representation of the circuit network. You can take advantage of the plethora of the Graph modules available in CPAN. They might actually help you in finding the connectivity of nodes and other stuff. This is of course if you are building a production worthy system out of this. If this is only for academic learning purposes you can stay with your own format and parser.
Hope this helps.
-T
use perl; use strict;
| [reply] |
It's just a small little thing I need to write for a class (academic learning purposes, you could say). But thanks for the help.
| [reply] |
Pity you don't need to take it further - a general-purpose circuit simulator in perl would be really nifty :-)
| [reply] |