I'm trying to create a circuit from a text file. The format is that wire is represented as a '.' and a capacitor is represented by a number (0-9). (So far this is all that is supported.) The first element must be a battery, and the positive lead goes to the right, the negative goes down.
In reading in the file, I transfer it to an array. However, when I later try to access the array via
$Circ->[$i][$j], I cannot test this for equality -- I print out the value of
@{$Circ->[$i][$j]} and it is '.', but when I ask if
@{$Circ->[$i][$j]} eq '.' (or == '.' or =~ /\./) I get a negative. Here is a snippet.
# read in file, make circuit ...
use Data::Dumper;
# 012345678901 <--(i)-->
# 0 B+.....1....
# | 1 - .
# (j) 2 . .
# | 3 . .
# 4 .........2..
# =======================
# C1:5 (for 5 uF)
# C2:6
open (CIRC, "circ.txt");
$i = 0; $j = 0;
while (<CIRC>) { # put elements of circuit into m-d array
last if $_ =~ /=+/;
chomp;
$i = 0;
for $char (split //, $_) { push @{$Circ->[$i++][$j]}, $char }
$j++;
}
$i = 1; $j = 0;
while (1) {
# count caps, make sure > 1
$on_i = $i; $on_j = $j;
# make conditions for going up, dwn, lft, rt...
if ($j-1 > 0) { LookAt ($i,$j-1) } # look up
if ($j+1 < scalar @{$Circ->[$i]}-1) { LookAt ($i,$j+1) }
# look down
if ($i-1 > 0) { LookAt ($i-1,$j) } # look left
if ($i+1 < scalar @$Circ-1) { LookAt ($i+1,$j) }
# look up
}
sub LookAt {
$l_i = $_[0];
$l_j = $_[1];
if (@{$Circ->[$l_i][$l_j]} eq '.') { print "i'm here" }
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.