sashac88 has asked for the wisdom of the Perl Monks concerning the following question:

Hello again,good monks.
I have this problem:
I read all lines from one file to array.Then for each item of array I search the second file.If the line was found -success,otherwise failure.So far everything's OK.
If the item of array (i.e. the line in first file) has strange characters-it doesn't find it in the second file.
Here's an example of such line:
sntp authentication-key 3836359425 md5 2i[
another example:
rmon event 10045 none community 3l&n|&#/0})lrk'xhj4)v8g!8]@kw&|5rm5o+l +$t'_appkgqnr9}#&e description
When I execute the script in command line-I see many lines with this message:
Unmatched ) in regex; marked by <-- HERE in m/^rmon event 10045 none c +ommunity 3 l&n|&#/0}) <-- HERE lrk'xhj4)v8g!8]@kw&|5rm5o+l$t'_appkgqnr9}#&e descr +iption/ at
Please help.Thanks in advance.

Replies are listed 'Best First'.
Re: searching strings in text files
by Zaxo (Archbishop) on Jul 25, 2004 at 06:34 UTC

    It sounds like the immediate cause of the problem is unescaped regex metacharacters. You can fix that with quotemeta or /\Q$foo\E/ in the regex.

    From your description of the problem, it sounds like the eq operator might do better than a regex.

    Your algorithm seems to be quadratic. You would speed matters by taking each line, or an md5 digest of it, as a hash key. You would then only need to check existence of a key to find like lines.

    Update: Here is an example:

    my %filehash; { open my $fh, '<', '/path/to/file_one.txt' or die $!; $filehash{$_}++ while <$fh>; close $fh or die $!; } { open my $fh, '<', '/path/to/file_two.txt' or die $!; while (<$fh>) { print qq("$_" is not in file one\n) if not exists $filehash{$_}; } }

    After Compline,
    Zaxo

      Thank you very much for the quick responce.(By the way is there a way in this forum I can assign point or something like that).
      I guess you're right about the quadratic algorithm if it can be called algorithm anyway. Where can I read more about your comment?Could you give me an example of code...
      Thanks again.
Re: searching strings in text files
by davido (Cardinal) on Jul 25, 2004 at 06:26 UTC

    If you're using the lines of file 'A' as a regexp to match in file 'B', the problem you're experiencing is that most non-alpha characters have special meaning in regexps. What you need to do, if you require a regexp match based on the contents of file 'A', is to use quotemeta to prepare the search string so that its content won't be interpreted as regexp special characters.


    Dave

      Thanks a lot.It worked like miracle.Quick and great solution.
      Thanks.