By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,834 Members | 2,234 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,834 IT Pros & Developers. It's quick & easy.

Comparing two strings

P: 2
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
Share this Question
Share on Google+
2 Replies


P: 2
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
Expert 2.5K+
P: 4,059
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

Post your reply

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