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:
matching exact text as regex from variable

 



odorf
Novice

Oct 5, 2010, 11:59 AM

Post #1 of 14 (1779 views)
matching exact text as regex from variable Can't Post

hello guys. i ve been trying to write some code that will be doing the below stuff:

1.scan a file, and store in an array its contents line by line
2. do the same with a second file
3. matches every line of the second file with all the lines of the first using this code:


Code
if($arch[$w] =~ /$preg/)


4. if a match is found, do something else blah blah blah...

however it doesnt seem to work...

after some trial and error i came up to assume that the problem is in the line above. more specifically i think that there is a problem with the text inside the $preg, since it often contains characters like (/.*?) and perl thinks they are regexes. is there a way to make it understand that i wanna compare it with the exact text in $preg or should i be writing an additional program that will be converting a line to an acceptable regex?

sorry for my english, if i didnt manage to convey what i meant, properly. i am new to perl and i cant utilize it to the full...

ps: i ve changed the code to this:


Code
if($arch[$w] =~ m{$preg})


the ironic is that it doesnt work in this form, but it would do the job if i just copy and paste a pattern from the file directly. for example, this works:


Code
if($arch[$w] =~ m{b>EDiT / ResYncH B})


where $preg should be equal to : b>EDiT / ResYncH B

this is really frustrating. any ideas?


(This post was edited by odorf on Oct 5, 2010, 12:56 PM)


BillKSmith
Veteran

Oct 5, 2010, 12:59 PM

Post #2 of 14 (1770 views)
Re: [odorf] matching exact text as regex from variable [In reply to] Can't Post


Code
if($arch[$w] =~ /\Q$preg\E/) {



Refer: perldoc perlretut "Part 2: Power Tools"
Good Luck,
Bill


odorf
Novice

Oct 5, 2010, 1:01 PM

Post #3 of 14 (1769 views)
Re: [BillKSmith] matching exact text as regex from variable [In reply to] Can't Post

yeah... already tried that. nothing... :(

and it wouldnt work with that either:


Code
if($arch[$w] =~ /\Qb>EDiT / ResYncH B\E/)



7stud
Enthusiast

Oct 5, 2010, 1:34 PM

Post #4 of 14 (1765 views)
Re: [odorf] matching exact text as regex from variable [In reply to] Can't Post


Quote
the ironic is that it doesnt work in this form, but it would do the job if i just copy and paste a pattern from the file directly.


Prove it!


Code
use strict; 
use warnings;
use 5.010;


my @arch = (
'b>EDiT / ResYncH B hello world',
'hello world b>EDiT / ResYncH B',
);

my $preg = 'b>EDiT / ResYncH B';

for my $line (@arch) {
if ($line =~ /$preg/) {
say "$line";
}

}

--output:--
b>EDiT / ResYncH B hello world
hello world b>EDiT / ResYncH B



(This post was edited by 7stud on Oct 5, 2010, 1:39 PM)


odorf
Novice

Oct 5, 2010, 1:38 PM

Post #5 of 14 (1763 views)
Re: [7stud] matching exact text as regex from variable [In reply to] Can't Post

7stud this one works but i dont understand how i can implement it to my code. what am i doing wrong?


7stud
Enthusiast

Oct 5, 2010, 1:40 PM

Post #6 of 14 (1762 views)
Re: [odorf] matching exact text as regex from variable [In reply to] Can't Post

I don't know. You haven't provided an example that demonstrates it doesn't work.


(This post was edited by 7stud on Oct 5, 2010, 1:43 PM)


odorf
Novice

Oct 5, 2010, 1:42 PM

Post #7 of 14 (1760 views)
Re: [7stud] matching exact text as regex from variable [In reply to] Can't Post

this is the entire code:


Code
#!/usr/bin/perl 

system("ls > files");
open(FILES, "files");
@files = <FILES>;
$countfiles = @countfiles;
close(FILES);
unlink("files");
open(MOTIVS, "motivs");
@motifs = <MOTIVS>;
$countmotifs = @motifs;
close(MOTIVS);
$match = 0;
for($q=0 ; $q<=$countfiles ; $q++)
{
if($files[$q] =~ /^(.*srt)$/)
{
open(ARCH, "$1");
@arch = <ARCH>;
$countarch = @arch;
for($w=0 ; $w<=$countarch ; $w++)
{
for($e=0 ; $e<=$countmotifs ; $e++)
{
$test = $arch[$w];
$preg = $motifs[$e];
$preg =~ s{\/}{\\\/}g;
print "$preg";
if($arch[$w] =~ /$preg/)
{
$match++;
print "$w\n";
}
}
}
}
print "\n\n\nFound:\t$match\n\n";
}


when i try it, it constantly gives $match = 0 :S


7stud
Enthusiast

Oct 5, 2010, 1:45 PM

Post #8 of 14 (1756 views)
Re: [odorf] matching exact text as regex from variable [In reply to] Can't Post

You need to put aside that program and write a short practice program first. Your practice program should open one file that has three lines in it. Loop over the lines, and write a regex to match something in the lines. Print out the matches.


7stud
Enthusiast

Oct 5, 2010, 1:50 PM

Post #9 of 14 (1754 views)
Re: [7stud] matching exact text as regex from variable [In reply to] Can't Post

Also...

1) Start all your programs with the following lines:


Code
use strict; 
use warnings;
use 5.010; #if using perl 5.10+


2) Declare all variables with my(), e.g.:


Code
my $num; 
my @numbers;
my($num1, $num2) = (3, 4);
my %phone_number_for;



3) Use the 3-arg form of open(), and use a variable instead of a bareword filehandle:


Code
open my $INFILE, '<', 'data.txt' 
or die "Couldn't open data.txt: $!";




4) Loops are not written that way in perl. To loop over the lines of a file, you just write:


Code
for my $line (<$INFILE>) { 
#do something with $line
...
}


The loop will automatically end after the last line in the file.


(This post was edited by 7stud on Oct 5, 2010, 1:54 PM)


BillKSmith
Veteran

Oct 5, 2010, 1:55 PM

Post #10 of 14 (1751 views)
Re: [odorf] matching exact text as regex from variable [In reply to] Can't Post

You must identify the problem before you can fix it. I have a few suggestions, you probably have already done many of them.

\Q...\E does not protect $ and @. Is that a problem?

Use 'use strict;' and 'use warnings;'. They will not solve your problem, but may give insight.

Post a pair of lines which you believe match, but the program does not find.

Tell us if the program finds any of the matches. Post a pair of these if it does.

Are you sure that the files have the same style of newline? If not, has the problem been handled?

Verify with debug or with warn statements that both variables are correct at the time they are used?
Good Luck,
Bill


odorf
Novice

Oct 5, 2010, 1:57 PM

Post #11 of 14 (1751 views)
Re: [7stud] matching exact text as regex from variable [In reply to] Can't Post

ok there seems to be some light in the tunel:
this code:


Code
#!/usr/bin/perl -w 

use warnings;
use 5.010;

open(FILE, "22.Bullets.2010.1080p.BluRay.x264.DTS-WiKi.srt");
@file = <FILE>;
close(FILE);
open(MOTIV, "motivs");
@motif = <MOTIV>;
close(MOTIV);
$match = 0;
for $line (@file)
{
for $motiv (@motif)
{
if($line =~ /$motiv/)
{
$match++;
print "\n\n$match\n\n";
}
}
}


wont work, whereas this one works... :S


Code
#!/usr/bin/perl -w 

use warnings;
use 5.010;

open(FILE, "22.Bullets.2010.1080p.BluRay.x264.DTS-WiKi.srt");
@file = <FILE>;
close(FILE);
open(MOTIV, "motivs");
@motif = <MOTIV>;
close(MOTIV);
$match = 0;
$motiv = 'b>EDiT / ResYncH B';
for $line (@file)
{
if($line =~ /$motiv/)
{
$match++;
print "\n\n$match\n\n";
}
}


could the problem be that i load the pattern from a different file?


odorf
Novice

Oct 5, 2010, 2:04 PM

Post #12 of 14 (1748 views)
Re: [BillKSmith] matching exact text as regex from variable [In reply to] Can't Post


Quote
Are you sure that the files have the same style of newline? If not, has the problem been handled?


i dont really know what does this mean, and how can i figure it out, but the first file has been created in windows and the second one in linux, if that has anything to do with the newline or something?

ok, i have converted the files with dos2unix command, so i believe it's ok to suppose that they have the same newline style?


(This post was edited by odorf on Oct 5, 2010, 2:19 PM)


7stud
Enthusiast

Oct 5, 2010, 7:01 PM

Post #13 of 14 (1735 views)
Re: [odorf] matching exact text as regex from variable [In reply to] Can't Post


Quote
i dont really know what does this mean, and how can i figure it out, but the first file has been created in windows and the second one in linux, if that has anything to do with the newline or something?


A newline is an invisible character or series of characters at the end of every line in a file. When you are typing in a file and you hit return, you have just typed an invisible newline character.

Depending on what operating system the file was created on, the newline will be entered as either an invisible:

\r\n (windows)

or

\n (unix, mac)

A further twist is that perl recognizes whatever operating system that the perl program is running on, and perl does automatic conversions from that operating system's newlines to a plain old '\n'. So for instance, if your perl program is running on windows, and the program reads a file, perl will convert any \r\n character sequences in the file to a \n before presenting them to your program. Similarly, if you write a \n to the file, perl will convert the \n to \r\n before writing the output to the file.

So in order to understand the particulars of what's happening, you need to know what operating system the perl program is running on, and you need to know what operating system the files that your program reads were created on.


(This post was edited by 7stud on Oct 5, 2010, 8:05 PM)


BillKSmith
Veteran

Oct 5, 2010, 9:06 PM

Post #14 of 14 (1729 views)
Re: [odorf] matching exact text as regex from variable [In reply to] Can't Post

I am not familiar with dos2unix, but I agree it sure sounds like it is a program to fix the newlines. It may not work if the program you used to transfer the files tried to "fix" them for you. You said that you were successful using copy and paste. There probably are no newlines in the screen image. Your editer generates them correctlywhen you save. It is probably easier to track this down with an editer than with perl. Do you have an editer that displays a file in hex or at least displays all the control characters? You should not have a CR character (CTRL-M or HEX 0D). You may have to write a perl program to do this.
Good Luck,
Bill

 
 


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

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