Home: Perl Programming Help: Advanced:
string comparison in perl



sshiva
New User

Apr 8, 2005, 6:05 PM


Views: 3156
string comparison in perl

hello all

could anyone guide me as to how can i compare two strings in perl? what i need is a "string diff" code that compares two strings and returns the "difference" between the two

eg:

string1 = "this is a test";
string 2 = "this is a";

returns " test" as the result

any suggestions/ideas appreciated

warm regards

shiva


KevinR
Veteran


Apr 9, 2005, 11:41 AM


Views: 3154
Re: [sshiva] string comparison in perl

this should have been posted in the beginner forum, but here is one way:


Code
my $string1 = "this is a test"; 
my $string2 = "this is a";
(my $string3 = $string1) =~ s/$string2//;
print $string3;

-------------------------------------------------


sshiva
New User

Apr 12, 2005, 11:32 AM


Views: 3137
Re: [KevinR] string comparison in perl

thanks kevin. sorry about posting this here as i needed a quick response to my answer and thanks to you, i indeed got it. Smile

i have another query and would appreciate if you could suggest me a workaround.

i have this text file basically containing a lot of object ids (OID) that i need to extract. there is no standard format for this file and OID's appear everywhere. It would be nice if i could extract all these OID's and dump them into an array or maybe another text file

please advise

regards

shiva


sshiva
New User

Apr 12, 2005, 11:35 AM


Views: 3136
Re: [sshiva] string comparison in perl

i forgot to mention that the OID's have the following format.
19-0006f06f-e8c5-119c-929b-80c79891304d. its a combination of hex/dec digits


KevinR
Veteran


Apr 12, 2005, 4:49 PM


Views: 3126
Re: [sshiva] string comparison in perl

You will need to post some sample lines from the text file to look at so someone can try and make some suggestions.
-------------------------------------------------


chaoslawful
New User

Nov 10, 2005, 9:06 AM


Views: 3038
Re: [KevinR] string comparison in perl

well, Algorithm::LCSS would be a better way in my opinionSmile


KevinR
Veteran


Nov 10, 2005, 9:55 AM


Views: 3036
Re: [chaoslawful] string comparison in perl

very good, but maybe you could post your suggestion sooner next time? This thread is about 7 months old. But maybe better late than never. I never heard of that module but will take a look at it for future reference. Smile
-------------------------------------------------