LPM: RE: variable comparison not working

ken.rietz at asbury.edu ken.rietz at asbury.edu
Thu Jun 1 11:45:59 CDT 2000


Hi, Tom. I don't get into the office much now, so it takes a longer
to respond.

> Ok, I'm trying to do some simple input data checking on a script I'm
> writing.  $book is a string entered from the keyboard.  
> @books is a list of
> available books.  Both are chomped, so no new lines should be 
> present.  I'm
> doing something like this:
> 
> $valid = 0;
> foreach (@books) {
> 	if ($_ eq $book) {
> 		$valid = 1;
> 		last;
> }
> }
> 
> This never matches, even when $book is a value in @books.

I'd say that $book is not in @books, but I can't say why.
What I would do is do effectively an ASCII dump of @books
and $book:

print join("#", split(//, $book));

and the same in a loop for @books. You will probably find
the problem there.

> In 
> trying to
> figure out why, I added a print statement to it as shown:
> 
> $valid = 0;
> foreach (@books) {
> 	if ($_ eq $book) {
> 		$valid = 1;
> 		last;
> }
> print "$valid-$book-$_-\n";
> }
> 
> I get output like the following (I used dashes since none of 
> the variables
> should have them):
> 
> --galatians-galatians
> --galatians-ephesians
> --galatians-philippians
> ...
> 0-galatians-philemon-

There are problems counting dashes here.
1) If there are two dashes at the beginning of most lines,
then both $valid and $book are undef. That doesn't fit
your code at all.
2) The last line ought to end with a dash, since $_ will
be undef at the end of the @books loop. But then $valid
and $books now have values, somehow.

Try cutting and pasting your code. I have a feeling that
you types this in from memory after numerous different
variations, and got things a bit scrambled.

That, of course, assumes you haven't figured it out yet.

> 
> As if this is not interesting enough, I tried it on my Windows 95 box
> (running ActivePerl 5.6) and it worked fine (I'm writing and 
> was testing it
> on Linux).  However, my linux box gives the same results from 
> both Perl
> 5.005 and Perl 5.6.

My only thought is that Linux and Win might treat \n differently.
That does seem to be part of the problem.

Were the files created under the same system they are being used,
or did you create them on one and copy them to the other?

-- Ken Rietz



More information about the Lexington-pm mailing list