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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String comparison, don't understand why its failing for two vars 1

Status
Not open for further replies.

perl8rookie

Programmer
Apr 25, 2008
4
US
I am using following code to read a value out of a binary file and convert it to hex, read another value from an excel file. The two values are different but when I compare these values in perl the result is a match which does not make sense.

I have attached a link to download the excel, binary and script files for you to try it out. What changes can I make in script to have the both values in the same format so that the comparison is correct?

I would really appreciate if someone can point out what I am doing wrong. The command to run the script is:

> perl new.pl new.bin

# Code:
#---------------------------------------------------------
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';
$Win32::OLE::Warn = 3; # die on errors...
my $Excel = Win32::OLE->GetActiveObject('Excel.Application')
|| Win32::OLE->new('Excel.Application', 'Quit');

if(open(FILE, "<".$ARGV[0]))
{
# Converts bin file to hex string
binmode(FILE);
while(!eof(FILE))
{
$hex_String .= sprintf("\%02X", ord(getc(FILE)));
}
$hex_value = substr($hex_String,$index,4);
$hex_value =~ s/^0+//;
print "\nhex value from bin file: $hex_value";
close(FILE);

# Gets the value in (1,1) and saves it to excel_value
my $readBook = $Excel->Workbooks->Open("new.xls");
my $readSheet = $readBook->Worksheets(1);
$excel_value = $readSheet->Cells($readRow,1)->{'Value'};
print "\nA value read from excel file: $excel_value";

if ($hex_value == $excel_value)
{
print "\nComparison Failure, values match";
}
else
{
print "\nComparison Success, values don't match";
}
}

# All files are attached. Please help
 
You're doing a numeric compare ( == ) on those hex strings
You need to do a string compare ( eq )
Code:
  if( $hex_value eq $excel_value )
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top