in reply to Trouble with array of arrays
if (@recordset[$i] && (@recordset[$i]->[1] == "exit")){ ... }
Another change you might consider is to add use warnings; and use strict; (see warnings and strict) at the top of your script. warnings would have warned about a string comparison using the == numeric comparator. The expression @recordset[$i]->[1] == "exit" will only be true if @recordset[$i]->[1] (another warning) or better yet $recordset[$i]->[1] or even $recordset[$i][1] (no warning, but -> is redundant) is 0. (Update: In numeric context, a string like 'exit' or 'foo' or '' evaluates to 0.)
Update 1: Some example code (note: no warnings, but strictures enabled):
c:\@Work\Perl>perl -Mstrict -le "print 'A: equal' if 0 == 'exit'; print 'B: equal' if 'exit' == 'exit'; print 'C: equal' if 'foo' == 'exit'; print 'D: equal' if 1 == 'exit'; " A: equal B: equal C: equal
Update 2: In many cases, a warning is referred to as an 'error'; there are many examples of this on PerlMonks. But a warning is not an error; it is, well, a warning: something that looks odd to the Perl interpreter and that the interpreter is telling you about because you asked it to. As long as you don't mind having whatever STDERR points to fill up with umpteen warning messages, Perl is perfectly happy to soldier on. There are, however, some cases in which a warning alludes to a serious semantic error, as when 0 or 'foo' is numerically compared to 'exit' and found to be equal! Bottom line, especially if you're a Perl novice: always ask for warnings; always pay close attention to them. (And IMHO, always eliminate their source.)
Update 3: And I should have said this long since: the proper equality operators for string comparison are eq and ne (see Equality Operators in perlop).
|
|---|