Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > regex in perl (using variables)

Reply
Thread Tools

regex in perl (using variables)

 
 
Ala Qumsieh
Guest
Posts: n/a
 
      08-12-2005
Dario wrote:

> Yes, I know that the code is less then perfect. I have to write it in perl.
> I tried it on windows and it worked, but when I tried it on linux(perl
> version 5.8.4) i didn't work.


Can you elaborate more? What do you exactly mean by "didn't work"? did
it core dump? power off your PC? turn off your bedroom lights? And since
we're on the subject, what do you mean by "it worked" on windows? maybe
you had the wrong expectations.

--Ala
 
Reply With Quote
 
 
 
 
dario
Guest
Posts: n/a
 
      08-12-2005
By it worked I mean: It printed "something" and by it didn't work I mean :
it didin't match on $reg and it didn't print "something"(program normally
finished but it didn't do what i wanted it to do).
"Ala Qumsieh" <(E-Mail Removed)> wrote in message
news:0BSKe.758$(E-Mail Removed). ..
> Dario wrote:
>
> > Yes, I know that the code is less then perfect. I have to write it in

perl.
> > I tried it on windows and it worked, but when I tried it on linux(perl
> > version 5.8.4) i didn't work.

>
> Can you elaborate more? What do you exactly mean by "didn't work"? did
> it core dump? power off your PC? turn off your bedroom lights? And since
> we're on the subject, what do you mean by "it worked" on windows? maybe
> you had the wrong expectations.
>
> --Ala



 
Reply With Quote
 
 
 
 
dario
Guest
Posts: n/a
 
      08-12-2005
Paul Lalli wrote:
> Please modify your script so that it produces some debugging output, is
> strict- and warnings-compliant, and checks for errors with open(). If
> after doing this you are still seeing an error, feel free to post your
> new program here for further assistance.


I have made all the corection anyone said but it still doesn't do what i
wanted it to do(matches the $head_ with $reg ).
I didn't use your's,

> open my $NWRULE, '<' 'rule.spam' or die "Could not open rule.spam: $!";


Because i got an error:

String found where operator expected at t_svm.pl line 7, near "'<'
'rule.spam'"
(Missing operator before 'rule.spam'?)
syntax error at t_svm.pl line 7, near "'<' 'rule.spam'"
Execution of t_svm.pl aborted due to compilation errors.

So I used insted:

open(DATA, "<rule.spam") || die "can't open rule.spam";
while ( my $rule = <DATA> ) {
....sniped
}
close(DATA);

Can you please make a file called rule.spam and put in it :
new_1 head Subject: .*\.\.
new_2 head Subject: Get

It's very important to use a file because i have to use it so nothing else
works for me!
Other important thing is to test it on linux/perl (it works on windows what
really ****es me off). My version of perl is 5.8.4 ( This is perl, v5.8.4
built for i386-linux-thread-multi) and the platform is debian sarge.


The whole program is :

#!/usr/bin/perl
use strict;
use warnings;

my $head_ ="Subject: Get cheap v i a g r a ..... ";


open(DATA, "<rule.spam") || die "can't open rule.spam";
while ( my $rule = <DATA> ) {
print "New rule is: $rule \n";
my ($new_id, $dio, $reg);
if ( $rule =~ /(\S+) (\S+) (.+)/ ) {
$new_id=$1;
$dio=$2;
$reg=$3;
}

print "New id is: $new_id\n";
print "New dio is: $dio\n";
print "New reg is: $reg\n";
if ( $head_ =~ m/$reg/ ) {
print "Using reg from a file\n"; # it newer matches
}
if ( $head_ =~ m/Subject: .*\.\./ ) {
print "Using written regex\n"; # it always matches
}
if ( $head_ =~ m/Subject: Get/ ) {
print "Using written regex\n"; # it always matches
}

}
close(DATA);

The output is :

New rule is: new_1 head Subject: .*\.\.

New id is: new_1
New dio is: head
New reg is: Subject: .*\.\.
Using written regex
Using written regex
New rule is: new_2 head Subject: Get

New id is: new_2
New dio is: head
New reg is: Subject: Get
Using written regex
Using written regex


Dario


 
Reply With Quote
 
dario
Guest
Posts: n/a
 
      08-12-2005
Thanks everyone! I figured it out! Begginers mistake!
I didn't use "chomp";
Dario
"dario" <(E-Mail Removed)> wrote in message
news:ddfeaf$73h$(E-Mail Removed)...
> How do I make this work!!!
>
> $head_ ="Subject: Get cheap v i a g r a ..... ";
>
> #$rule is a variable which I used for reading text from a file!
> open (NWRULE, "<rule.spam");
> @new_rule=<NWRULE>;
> close (NWRULE);
>
> Then I did sometning like this :
> foreach $rule(@new_rule)
> {
> if($rule =~ /(\S+) (\S+) ([^\n]+)/)
> {
> $new_id=$1;
> $dio=$2;
> $reg=$3;
> if($head_ =~ m/$reg/)
> {
> print "something\n";
> }
> ....
> Content of a file rule.spam is :
> new_1 head Subject: .*\.\.
>
>



 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      08-12-2005
dario wrote:
> Thanks everyone! I figured it out! Begginers mistake!
> I didn't use "chomp";


Not using chomp() may be a typical beginners mistake, but I fail to see
how that would make a difference with respect to the problem you had.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      08-12-2005
dario wrote:
> Paul Lalli wrote:
> I have made all the corection anyone said but it still doesn't do what i
> wanted it to do(matches the $head_ with $reg ).
> I didn't use your's,
>
> > open my $NWRULE, '<' 'rule.spam' or die "Could not open rule.spam: $!";

>
> Because i got an error:
>
> String found where operator expected at t_svm.pl line 7, near "'<'
> 'rule.spam'"
> (Missing operator before 'rule.spam'?)
> syntax error at t_svm.pl line 7, near "'<' 'rule.spam'"
> Execution of t_svm.pl aborted due to compilation errors.


My error. I forgot the comma between '<' and 'rule.spam'. Apologies.

> So I used insted:
>
> open(DATA, "<rule.spam") || die "can't open rule.spam";
> while ( my $rule = <DATA> ) {
> ...sniped
> }
> close(DATA);


The fact that you should be using lexical filehandles rather than
global bareword filehandles notwithstanding, you should *definately*
never be using DATA as your filehandle, as that has a special meaning
in and of itself. You're breaking that special meaning.

> Can you please make a file called rule.spam and put in it :
> new_1 head Subject: .*\.\.
> new_2 head Subject: Get
>
> It's very important to use a file because i have to use it so nothing else
> works for me!


I have no idea what this has to do with anything else in this thread.

> Other important thing is to test it on linux/perl (it works on windows what
> really ****es me off). My version of perl is 5.8.4 ( This is perl, v5.8.4
> built for i386-linux-thread-multi) and the platform is debian sarge.


I don't have access to linux currently. However, the script you posted
below works just fine on solaris with perl v5.6.1.

Output:
New rule is: new_1 head Subject: .*\.\.

New id is: new_1
New dio is: head
New reg is: Subject: .*\.\.
Using reg from a file
Using written regex
Using written regex
New rule is: new_2 head Subject: Get

New id is: new_2
New dio is: head
New reg is: Subject: Get
Using reg from a file
Using written regex
Using written regex


Paul Lalli

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      08-12-2005
dario wrote:
> Thanks everyone! I figured it out! Begginers mistake!
> I didn't use "chomp";


There is no place in the code you posted where results would have
differed by the use of a chomp. You must have made some other change,
either without realizing it, or without telling us. As I noted in my
previous posting, the code you posted works correctly.

Paul Lalli

 
Reply With Quote
 
dario
Guest
Posts: n/a
 
      08-12-2005
I don't know why but on my computer it really doesn't work if I don't use
chomp(like chomp($rule)). It has something to do with how linux, debian or
perl or whatever handles the newlines! This is why it works on windows
because windows have different way of handling newlines(I think). But I
think it has to work on all platforms. I posted my output from running the
program so that's really what i got.
Dario


"Gunnar Hjalmarsson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> dario wrote:
> > Thanks everyone! I figured it out! Begginers mistake!
> > I didn't use "chomp";

>
> Not using chomp() may be a typical beginners mistake, but I fail to see
> how that would make a difference with respect to the problem you had.
>
> --
> Gunnar Hjalmarsson
> Email: http://www.gunnar.cc/cgi-bin/contact.pl



 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      08-12-2005
dario wrote:
> Gunnar Hjalmarsson wrote:
>> Not using chomp() may be a typical beginners mistake, but I fail to see
>> how that would make a difference with respect to the problem you had.

>
> I don't know why but on my computer it really doesn't work if I don't use
> chomp(like chomp($rule)). It has something to do with how linux, debian or
> perl or whatever handles the newlines! This is why it works on windows
> because windows have different way of handling newlines(I think).


One thought is that you didn't convert the file in question to Unix
format when copying it from Windows. In that case the chomp()ing may
accidentally serve the purpose of removing the \r character.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      08-14-2005
dario wrote:

> So I used insted:
>
> open(DATA, "<rule.spam") || die "can't open rule.spam";


You took "$!" out of the die() message. That's not good.
Put it back in.

> Other important thing is to test it on linux/perl (it works on windows what
> really ****es me off).


Did you remember to run dos2unix on the file that you copied from
Windows to Linux? If the file with the rules has a carriage-return
(^M) at the end of each line, then it won't work as expected.

Either use ASCII mode when using FTP to copy the file, or use
dos2unix on Linux to fix the error.
-Joe
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
String Pattern Matching: regex and Python regex documentation Xah Lee Java 1 09-22-2006 07:11 PM
Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine? =?Utf-8?B?SmViQnVzaGVsbA==?= ASP .Net 2 10-22-2005 02:43 PM
Java regex imposture re: Perl regex compatibility a_c_Attlee@yahoo.com Java 2 05-06-2005 12:16 AM
perl regex to java regex Rick Venter Java 5 11-06-2003 10:55 AM



Advertisments