Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

odd behavior of strings

Status
Not open for further replies.

peterworth

Programmer
Aug 18, 2004
80
GB
The following code:

Code:
foreach $postcode (<NEEDED>)
{
   chomp $postcode;
   chop $postcode;
   print "'$postcode'";
   if ($postcode eq "SA460EY")
   {
      print "\nequal";
   }
   else
   {
      print "\nnot equal";
   }
}

gives output:
'SA460EY'
not equal

although clearly the value of $postcode printed IS equal to the one in the if statement. i appreciate its difficult to say whats wrong without seeing the NEEDED file, but for what reasons could these 2 strings not be equal despite appearing to be? could there be some kind of hidden character in there?

thanks.
 
Hello Peter,

Leave the chomp line in, remove the chop line. That will sort it out (just tried it)

Mike

To err is human,
but to really foul things up -
you require a man Mike.

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
hi,

now i just get this output:

'SA460EY
'ot equal
'SA460DL
not equal

notice the second ' dissapears for some reason...

thanks for your help.

p.s. the file its reading from is just a list of postcodes 1 per line. eg the last 2 which give the output above are just:

SA460EY
SA460DL
 
Mike's advice resolved the issue per my testing as well. Here's my test.

Code:
foreach $postcode (<DATA>)
{
   chomp $postcode;
   print "'$postcode'\n";
   if ($postcode eq "SA460EY")
   {
      print "equal\n";
   }
   else
   {
      print "not equal\n";
   }
}

__DATA__
SA460EY
SA460DL

--jim
 
ok thanks - i realise now that the code should work, its just that there are 'O's where there should be '0's :/ sorry..
 
Time to switch fonts on your terminal. :) I use Monaco Regular. Zeroes have a slash through them, O's don't. Plus it's fixed width, some people don't like that though.

--jim
 
Fixed width, best for coding anyway (formatting), Lucida Console
--Paul

Nancy Griffith - songstress extraordinaire,
and composer of the snipers anthem "From a distance ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top