473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Comparing two strings

I have a subroutine that is intended to check if a string is contained anywhere within another string.

Expand|Select|Wrap|Line Numbers
  1. compare_strings($string1,$string2);
  2.  
  3.  
  4. sub compare_strings
  5. {
  6.     my ($searchString,$dataString) = @_;
  7.     my (@searchString) = $searchString;
  8.     my (@dataString) = $dataString;
  9.     for ($i = 0;$i < @dataString - @searchString;$i++)
  10.     {
  11.         MISMATCH: for ($j = 0;$j < @searchString;$j++)
  12.         {
  13.             if($searchString[$j] ne $dataString[$i+$j])
  14.             {
  15.                 last MISMATCH;
  16.             }
  17.             if ($j+1 == @searchString)
  18.             {
  19.                 return 1;
  20.             }
  21.         }
  22.     }
  23.     return 0;
  24. }
The first for loop is to cycle until the last letter of @searchString is tested against the last letter of @dataString. The second for loop cycles for each letter of @searchString. In the event a mismatch is found the second loop is exited via last. If the entire @searchString is matched the subroutine returns 1.

It seems straightfoward to me but never returns 1, even when I know it should. I feel the algorithm is correct and the problem is instead a syntax error. Perhaps in the way the strings are passed to the subroutine.

Please, help....I'm running out of hair to pull out!

Perhaps we can see what data is being used as well? Could you please post that?

Regards,

Jeff
Jul 11 '08 #1
2 1740
string1 is parsed from an HTML form and string2 is read from a file. Here is some addition code inwhich the previous is used.

Expand|Select|Wrap|Line Numbers
  1.         open (BOOKS, "$BooksFile") || die print "search() unable to open $BooksFile for read";
  2.         my (@books) = <BOOKS>;
  3.         close (BOOKS);
  4.         my ($book);            # holds one line from books.dat
  5.         my (@bookInfo);            # holds each element in an array from above scaler
  6.         @book_write;            # holds each matched line as elements in an array
  7.         $numOfBooks = -1;
  8.         $numOfMatches = 0;
  9.         foreach $book (@books)
  10.         {
  11.             @bookInfo = split(/\|/, $book);
  12.             if (compare_strings($title,$bookInfo[2]) && $bookInfo[1] != 1000000) #   || compare_strings($author,$bookInfo[3]) || compare_strings($isbn,$bookInfo[4])
  13.             {
  14.                 push(@book_write, "$book\n");
  15.                 $numOfMatches++;
  16.             }
  17.             $numOfBooks++;
  18.         }
For simplicity's sake, I've commetted two other uses of the compare_strings subroutine.

Consider this for the data in $BooksFile

Expand|Select|Wrap|Line Numbers
  1. 1000000|1000000|title|author|isbn|price|
  2. 1000001|1000001|test|test author 1|test isbn 1|test price 1|
  3. 1000001|1000002|test title 2|test author 2|test isbn 2|test price 2|
  4. 1000003|1000009|new test|test|test|test|
If $title is equal to "new" the forth line in the data should be pushed into @book_write.


How did you post within my original post???
Jul 11 '08 #2
KevinADC
4,059 Expert 2GB
string1 is parsed from an HTML form and string2 is read from a file. Here is some addition code inwhich the previous is used.

Expand|Select|Wrap|Line Numbers
  1.         open (BOOKS, "$BooksFile") || die print "search() unable to open $BooksFile for read";
  2.         my (@books) = <BOOKS>;
  3.         close (BOOKS);
  4.         my ($book);            # holds one line from books.dat
  5.         my (@bookInfo);            # holds each element in an array from above scaler
  6.         @book_write;            # holds each matched line as elements in an array
  7.         $numOfBooks = -1;
  8.         $numOfMatches = 0;
  9.         foreach $book (@books)
  10.         {
  11.             @bookInfo = split(/\|/, $book);
  12.             if (compare_strings($title,$bookInfo[2]) && $bookInfo[1] != 1000000) #   || compare_strings($author,$bookInfo[3]) || compare_strings($isbn,$bookInfo[4])
  13.             {
  14.                 push(@book_write, "$book\n");
  15.                 $numOfMatches++;
  16.             }
  17.             $numOfBooks++;
  18.         }
For simplicity's sake, I've commetted two other uses of the compare_strings subroutine.

Consider this for the data in $BooksFile

Expand|Select|Wrap|Line Numbers
  1. 1000000|1000000|title|author|isbn|price|
  2. 1000001|1000001|test|test author 1|test isbn 1|test price 1|
  3. 1000001|1000002|test title 2|test author 2|test isbn 2|test price 2|
  4. 1000003|1000009|new test|test|test|test|
If $title is equal to "new" the forth line in the data should be pushed into @book_write.


How did you post within my original post???
You need to clearly explain what you are trying to match in order to get specific help. If you are matching substrings you will want to use index() or a regexp to search for matches. If you are searching for patterns you must use a regexp. If you are searching for identical strings then use "eq".

Unless the compare_strings() subroutine is used by other parts of your program there is no need to make it a seperate function. That is just complicating matters for now because it is evident by your code you are not too familiar with perl and maybe not familiar with programming in general. Kudos for trying to write the code, I respect a person that puts in the effort.
Jul 11 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: beliavsky | last post by:
By mistake I coded something like print ("1" > 1) and got the result "True". Comparing an integer and a string seems meaningless to me, and I would prefer to have an exception thrown. Can...
26
by: William Park | last post by:
How do you compare 2 strings, and determine how much they are "close" to each other? Eg. aqwerty qwertyb are similar to each other, except for first/last char. But, how do I quantify that? ...
5
by: Curtis Gilchrist | last post by:
I am required to read in records from a file and store them in descending order by an customer number, which is a c-style string of length 5. I am storing these records in a linked list. My...
4
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three...
6
by: BrianJones | last post by:
I have a problem with the int strcmp(str1,str2) function: When I do: char *pass; char *passv; pass = getpass("Please enter....."); passv = getpass("Please verify.....");
88
by: William Krick | last post by:
I'm currently evaluating two implementations of a case insensitive string comparison function to replace the non-ANSI stricmp(). Both of the implementations below seem to work fine but I'm...
2
by: Manny Chohan | last post by:
Hi, i have two datetime values in format 11/22/04 9:00 AM and 11/22/04 9:30 AM. How can i compare dates .net c# or if there is any other way such as Javascript. Thanks Manny
15
by: luc.saffre | last post by:
Hello, here is something that surprises me. #coding: iso-8859-1 s1=u"Frau Müller machte große Augen" s2="Frau Müller machte große Augen" if s1 == s2: pass
1
by: Jetboy555 | last post by:
Sample input: 2000 Georgia Tech 30 Virginia 20 1999 Virginia 20 Virginia tech My Problem is in taking the input in correctly. I take the year in correctly, but i'm having trouble with the...
2
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.