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:
help with regex

 



winfinit
User

Jul 16, 2007, 12:35 PM

Post #1 of 7 (594 views)
help with regex Can't Post

hi guys, i am a little bit confused and cant find my mistake here, in sub REMOVE i am performing expression on extracting line number form the entry, i am using same regex in sub HEART ($array[$counter]=~m/^([\d]*):/g;
$LineNumber=$1) and it is working great here, but for some reason it is not working in remove sub, i know that array entry exist, because if i am just trying to pring array entry, it is working fine.

every array entry is a first line of an email spool with added line number in front of it

3684:From email@example.com Tue Jul 3 10:18:17 2007

so this:

$array[$msg]=~m/^([\d]*):/g;
print $1;

is not producing any output, why?

-------------------------------------------

#!/usr/bin/perl

print "ENTER SPOOL FILE NAME: ";
chomp($spool=<STDIN>);

if(-e $spool)
{&MAIN;
&HEART;
&REMOVE;}

else
{print"SPOOL DOESNT EXIST\n";
exit;}

sub MAIN
{
if(-z $spool)
{print"SPOOL IS EMPTY\n";
exit;}

@array=`egrep -n 'From [A-Za-z0-9.-_]*@[A-Za-z0-9.-_]*' $spool`;

}

sub HEART
{
$counter=0;
$size=@array;

while($counter<$size)
{$array[$counter]=~m/^([\d]*):/g;
$LineNumber=$1;

$array[$counter]=~m/([JFMASOND][aepuco][nbrylgptvc])[\s]*([\d]*)/g;
$Month=$1;
$Day=$2;

$array[$counter]=~m/([\d]*$)/g;
$Year=$1;

$msgnum=$counter+1;
print "$msgnum) $LineNumber: $Day $Month $Year\n";
$counter+=1;}
}

sub REMOVE
{
print "\nEnter a number of messages to remove(ctrl+c to exit): ";
chomp($msg=<STDIN>);
$msg=$msg-1;

$array[$msg]=~m/^([\d]*):/g;
print $1;

}


KevinR
Veteran


Jul 16, 2007, 1:14 PM

Post #2 of 7 (592 views)
Re: [winfinit] help with regex [In reply to] Can't Post

add:


Code
use warnings;


to your script. Run the script and see if you get any warnings.
-------------------------------------------------


winfinit
User

Jul 16, 2007, 1:31 PM

Post #3 of 7 (591 views)
Re: [KevinR] help with regex [In reply to] Can't Post

here is output:

root@box:~/scripts/EXPRESSION# perl expressionV.1
ENTER SPOOL FILE NAME: INBOX
1) 1: 23 Jul 2007
2) 1811: 3 Jul 2007
3) 3593: 3 Jul 2007
4) 3638: 3 Jul 2007
5) 3684: 3 Jul 2007
6) 3754: 4 Jul 2007
7) 4720: 4 Jul 2007
8) 7898: 4 Jul 2007
9) 7979: 4 Jul 2007

Enter a number of messages to remove(ctrl+c to exit): 4
3638:From email@emaple.com Tue Jul 3 10:11:57 2007
Use of uninitialized value in print at expressionV.1 line 55, <STDIN> line 2.

=====================================

here is code:

47 sub REMOVE
48 {
49 print "\nEnter a number of messages to remove(ctrl+c to exit): ";
50 chomp($msg=<STDIN>);
51 $msg=$msg-1;
52
53 $array[$msg]=~m/^([\d]*):/g;
54 print $array[$msg];
55 print $1;
56
57 }


=============================

in fact, i forgot to tell you, if i am putting that sub as a sep script it iw working fine here is example



testing:
root@box:~/scripts/EXPRESSION# cat > test << "EOF"
> 3638:From test@example.com Tue Jul 3 10:11:57 2007
> EOF
==============================================

only remove sub:

#!/usr/bin/perl
@array=`cat test`;

print "\nEnter a number of messages to remove(ctrl+c to exit): ";
chomp($msg=<STDIN>);
$msg=$msg-1;

$array[$msg]=~m/^([\d]*):/g;
print $array[$msg];
print "$1\n";
=================================================

OUTPUT:

root@box:~/scripts/EXPRESSION# perl testepr

Enter a number of messages to remove(ctrl+c to exit): 1
3638:From test@example.com Tue Jul 3 10:11:57 2007
3638
root@box:~/scripts/EXPRESSION#



so i am not sure where my data is getting lost, or what portion of my code is creating confusion...


winfinit
User

Jul 16, 2007, 2:06 PM

Post #4 of 7 (588 views)
Re: [winfinit] help with regex [In reply to] Can't Post

ok god it, i think it is a perl bug, i dont see any coding issue on my side just by moreving /m/.../g; and using /..../ example below, gave me desired results

root@box:~/scripts/EXPRESSION# perl expressionV.1
ENTER SPOOL FILE NAME: INBOX
1) 1: 23 Jul 2007
2) 1811: 3 Jul 2007
3) 3593: 3 Jul 2007
4) 3638: 3 Jul 2007
5) 3684: 3 Jul 2007
6) 3754: 4 Jul 2007
7) 4720: 4 Jul 2007
8) 7898: 4 Jul 2007
9) 7979: 4 Jul 2007

Enter a number of messages to remove(ctrl+c to exit): 5
3684
3684:From test@example.com Tue Jul 3 10:18:17 2007



CODE:

47 sub REMOVE
48 {
49 print "\nEnter a number of messages to remove(ctrl+c to exit): ";
50 chomp($msg=<STDIN>);
51 $msg=$msg-1;
52
53 $array[$msg]=~/^([\d]*):/;
54 print "$1\n";
55 print $array[$msg];
56 }

========================================

line 53 is the one that i moded it used to be $array[$msg]=~m/^([\d]*):/g;


KevinR
Veteran


Jul 16, 2007, 4:17 PM

Post #5 of 7 (579 views)
Re: [winfinit] help with regex [In reply to] Can't Post

hehehe... sure blame it on perl. The "g" modifier should not even be in your regexps since it appears you are matching only the beginning of one line so there can only be one match, not multiple matches, which is what the "g" modifier is for. But it also will not hurt anything to leave it in. The "m" is also not needed since perl knows you are doing a matching regexp and you are using the default delimiters: //. But keeping the "m" in will also not hrt anything

This works:


Code
my @array =('3684:From email@example.com Tue Jul 3 10:18:17 2007'); 
$array[0]=~m/^([\d]*):/g;
print $1; #prints 3684


which is essentially your original code, so the problem is else where.

BTW, \d is a character class so putting it inside square brackets [] is not doing anything.
-------------------------------------------------


(This post was edited by KevinR on Jul 16, 2007, 4:19 PM)


winfinit
User

Jul 16, 2007, 4:45 PM

Post #6 of 7 (576 views)
Re: [KevinR] help with regex [In reply to] Can't Post

well, if you would read the whole thing, then you would of save yourself some time, thats exactly what i said above, if you would put that sub remove into a separate script, it would work just fine, and yes i do know what g means, and yes i do know what m means, that was not a part of my question, i use g in every single expression of mine, this is just the way i got used to it, and it never hurted me untill now.

code works perfectly fine if i will remove global match, so obviously there is a problem in that expression, even warnings are pointing out to that line.

just by placing global match i am not getting value pushed into $1... ehhh whatever I fixed my issue, my code is worknig now, so thats all i care about, thanks for the follow up


KevinR
Veteran


Jul 16, 2007, 10:28 PM

Post #7 of 7 (572 views)
Re: [winfinit] help with regex [In reply to] Can't Post

Sorry if my comments irritated you, they were not meant to. I found your observation of a bug in perl to be funny because your code is poorly written and is obviously prone to be buggy because of that. Using the "g" modifier in all your regexp is just silly, even more so if you know what it means. I assumed you did not understand what it meant since it is being used improperly in your code.

But you seem to be content now that your code is working so I see no reason to comment further on your code.
-------------------------------------------------

 
 


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

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