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

How to compare strings OR chars of 2 strings

Status
Not open for further replies.

bruceleung2000

IS-IT--Management
Dec 20, 2003
19
HK
Hello ALL,
I am a newbie to perl programming. I am trying to compare 2 strings read from separate files. It seems it the results are the same - cannot compare 2 same strings.

Can anyone tell me how to correct the following programs ?

=====
open (fh0, $fname0) or die "cannot read file $fname0" ; # $_[0] is first argument in the function
open (fh1, $fname1) or die "cannot read file $fname1" ;

sysread fh0, $instr0, $MAX_READ;

sysread fh1, $instr1, $MAX_READ;

if ( $instr0 =~ $instr1 ) { # ??????
print "eqqqqq char \n" ;
close fh0;
close fh1;
return $TRUE;
} # endif
close fh0;
close fh1;
print "NOTTTTTTTTT EQ char \n" ;
return $FALSE;
==================================
 
You want to check if the strings are the same right?
Code:
if ( $instr0 eq $instr1 ) {
   # do something
}
Use "eq" to compare strings, or "==" to compare numbers.
 
I used EQ before, but it seems not working well.

Regards,
Bruce
 
[MORE INFO for my problem]

It always NOT equal and also hits some strange error as follows :
=========================================
444444444]instr0=[111111111111xxxxxxx1111111111111111111111111111111111111111111
11111111111111111
222222222222222
3333333333333333333333333333333333333333
4444444444]
instr1=[111111111111111111111111111111111111111111111111111111111111111111111111

2222222yyyyyyyy22222222
3333333333333333333333333333333333333333
444444444]
NOTTTTTTTTT EQ char
inside for loop IIIII
i=[3]

G:\Schedule_Jobs\appletemp>perl ok2.pl *.html > ok3.out
Use of uninitialized value in print at ok2.pl line 62.
Use of uninitialized value in print at ok2.pl line 66.
Unmatched [ in regex; marked by <-- HERE in m/<HTML> ?????
<BODY>
<HEAD> Ver 4.1 developed by Bruce Leung on 27 July 2003 <BR> </HEAD>
??? <-- HERE ??????<BR>
????????????????????/ at ok2.pl line 42.

======================

Regards,
Bruce
 
Assuming your files are text files, 'eq' (not 'EQ' - case is important) will work OK. To test, try setting $fname0 and $fname1 to the same filename, in which case your two strings will definitely be equal.
 
What's on line 42, where the error is occurring?
 
Sorry you are right - my mistake.

BTW, how can I
1) read strings from a file in a posstion relative to END of file
2) read a line instead of fixed number of chars

Regards,
Bruce
 
Reading from the end of a file is dealt with in this node on perlmonks

To read a single line:
Code:
my $line = <fh0>;
To continually read lines in a loop:
Code:
while( my $line = <fh0>) {
   # do something
}

#or

while (<fh0>) {
   # the line is now in the $_ variable
}
 
Hello,
What I want is :
1) to skip frist 5 lines for the 2 following files (fh0 and fh1) and need to mix with function (sysread) to read before the sysread buffer.


============================================
open (fh0, $fname0) or die "cannot read file $fname0" ;
open (fh1, $fname1) or die "cannot read file $fname1" ;
??? how to skip 1st 5 lines for both files ???
sysread fh0, $instr0, $MAX_READ;
sysread fh1, $instr1, $MAX_READ;
================================================

Regards,
Bruce


 
Hello ALL,
It would be best for me to locate the position from end of files (NOT begin) to compare the strings of files.

Regards,
Bruce
 
You could read the whole file into an array with
Code:
open (FH0, $fname0) or die "can't open $fname0";

my @file0 = (<FH0>);
chomp @file0; # remove trailing CRLFs
then you can just work backwards from the end of the array?
 
That would work, but is inefficient for memory purposes as it slurps the whole file, as you said. The File::ReadBackwards achieves the same thing but is more memory-efficient.
 
True, although it seems to be HTML of some sort. I guess if it's small enough to display in someone's browser, it's not going to be too much of a hog...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top