CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Basic question - match against \o from input file

 



relaxing
New User

Dec 18, 2013, 6:21 AM

Post #1 of 8 (1514 views)
Basic question - match against \o from input file Can't Post

Gurus,

Please excuse my ignorance but I am unable to resolve this issue.

Following code gives me compilation error (Missing braces on \o{}) - is it asking me to include an escape char for \o?

I will be getting this data as is from a file for matching purposes.

code:

$match = "Hello \orld";

Quote


(This post was edited by relaxing on Dec 18, 2013, 6:39 AM)


FishMonger
Veteran / Moderator

Dec 18, 2013, 6:43 AM

Post #2 of 8 (1511 views)
Re: [relaxing] Basic question - match against \o from input file [In reply to] Can't Post

Looks like you have a simple typo.

Instead of:

Quote
$match = "Hello \orld";


You probably meant:

Quote
$match = "Hello World";



relaxing
New User

Dec 18, 2013, 6:52 AM

Post #3 of 8 (1509 views)
Re: [FishMonger] Basic question - match against \o from input file [In reply to] Can't Post

No, basically I am trying to match data from 2 files. Both the files have a record with the data as "Hello \orld". I tried to match it but it gave an error. I then tried to replicate it by the code $match = "Hello \orld" which gave the same run time error.


FishMonger
Veteran / Moderator

Dec 18, 2013, 6:56 AM

Post #4 of 8 (1506 views)
Re: [relaxing] Basic question - match against \o from input file [In reply to] Can't Post

Start by changing your quotes. Use single quotes so that perl won't interpolate the \o char.

Code
$match = 'Hello \orld';


If that doesn't solve the problem, then you'll need to post more code and info. i.e., post a short but complete script that demonstrates the problem and explain how the results differ from what you expect.


relaxing
New User

Dec 18, 2013, 7:15 AM

Post #5 of 8 (1503 views)
Re: [FishMonger] Basic question - match against \o from input file [In reply to] Can't Post

Thanks Ron. I think I understood the issue now. Input file has Hello \orld in double quotes, hence perl interpreting \o. I have given code and file contents below.
What is the best solution in this case..should I replace all " with another char, compare and then change it back or?

code:

Quote
open(file1, "<file1.txt") || die ("Cannot open input file1\n");
open(file2, "<file2.txt") || die ("Cannot open file2\n");
while ($file1line = <file1>)
{
chomp($file1line);
while ($file2line = <file2>) {
if($file2line =~ /$file1line/) {print "Match in file\n" } else {"No match in file\n"};
}
close file2;
}
close file1;
close file2;

File1 contents:
"Hello \orld"

File2 contents:
"Hello \orld"


(This post was edited by relaxing on Dec 18, 2013, 7:17 AM)


FishMonger
Veteran / Moderator

Dec 18, 2013, 7:32 AM

Post #6 of 8 (1498 views)
Re: [relaxing] Basic question - match against \o from input file [In reply to] Can't Post

Do you need to look for exact matches, or just if the line in file1 is a substring of a line in file2?

My guess is that you want exact matches.

Load the first file into a hash, then as you loop over the second file you can do a simple hash lookup to see if the line of the second file is a key in the hash.


Kenosis
User

Dec 18, 2013, 8:15 AM

Post #7 of 8 (1496 views)
Re: [relaxing] Basic question - match against \o from input file [In reply to] Can't Post

In cases where you may need to match regex metacharacters, you can quotemeta in your regex:

Code
if ( $file2line =~ /\Q$file1line\E/ ) { ...



relaxing
New User

Dec 18, 2013, 9:22 AM

Post #8 of 8 (1494 views)
Re: [Kenosis] Basic question - match against \o from input file [In reply to] Can't Post

Thank you Ron and Kenosis - it works now as expected.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives