[26302] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 8481 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Oct 4 03:05:27 2005

Date: Tue, 4 Oct 2005 00:05:05 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Tue, 4 Oct 2005     Volume: 10 Number: 8481

Today's topics:
    Re: Net::SMTP - sending email header ? <uctraing@ultranet.com>
        Simple pattern matching negation <uctraing@ultranet.com>
    Re: Simple pattern matching negation <todd@tdegruyl.com>
    Re: Simple pattern matching negation <1usa@llenroc.ude.invalid>
    Re: Simple pattern matching negation <sbryce@scottbryce.com>
    Re: Simple pattern matching negation <tadmc@augustmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Tue, 04 Oct 2005 02:28:23 GMT
From: Bob <uctraing@ultranet.com>
Subject: Re: Net::SMTP - sending email header ?
Message-Id: <78q3k1pnh3a2lthvq793sius6tk4qq2o6s@4ax.com>

On 03 Oct 2005 13:51:34 GMT, Villy Kruse
<vek@station02.ohout.pharmapartners.nl> wrote:

>Example from the MIME::Lite documentation. (perldoc MIME::Lite).
>
>           ### Do something like this in your 'main':
>           if ($I_DONT_HAVE_SENDMAIL) {
>              MIME::Lite->send('smtp', "smtp.myisp.net", Timeout=>60);
>           }
>
>           ### Now this will do the right thing:
>           $msg->send;         ### will now use Net::SMTP as shown above
>
>
>
>Villy

Thanks all. Time to study & implement. 






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

Date: Tue, 04 Oct 2005 02:33:55 GMT
From: Bob <uctraing@ultranet.com>
Subject: Simple pattern matching negation
Message-Id: <tcq3k19agkhbp2lhi78jimdgpqq1pc5kvg@4ax.com>

I'm a definite newbie at Perl and I need some basic syntax help. 

This works (checking for 4 numeric digits and doing something if
it's _not- true:

	if ($zip5=~m/[0-9]{5}/ != 1) { print ("invalid zip code");}

but I know there must be an easier syntax to negate the expression
$zip5=~m/[0-9]{5}/ within the if statement. I tried a couple of 
things that I thought should work but I am missing something really
basic in the use of "not" or "!". 

Thanks, 





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

Date: Mon, 03 Oct 2005 21:57:08 -0500
From: Todd de Gruyl <todd@tdegruyl.com>
Subject: Re: Simple pattern matching negation
Message-Id: <slrndk3rs4.1uj.todd@ibook.local>

On 2005-10-04, Bob <uctraing@ultranet.com> wrote:
> This works (checking for 4 numeric digits and doing something if
> it's _not- true:

I'm assuming you mean 5 digits.

> 	if ($zip5=~m/[0-9]{5}/ != 1) { print ("invalid zip code");}
>
> but I know there must be an easier syntax to negate the expression
> $zip5=~m/[0-9]{5}/ within the if statement. I tried a couple of 
> things that I thought should work but I am missing something really
> basic in the use of "not" or "!". 

if($zip5 !~ /[0-9]{5}/){
	print "invalid zip code";
}

or (depending on your needs)
	print "invalid zip code" unless $zip5 =~ /\d{5}/;
or:
	print "invalid zip code" if $zip5 !~ /\d{5}/;

the !~ operator is documented in perldoc perlop, the alternate
if/unless statement modifiers are documented in perldoc perlsyn (look
for 'Statement Modifiers').

[You'll also note that I used \d instead of [0-9], same thing... a
couple of characters shorter.]
-- 
Todd de Gruyl
todd@tdegruyl.com
http://www.tdegruyl.com


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

Date: Tue, 04 Oct 2005 03:02:23 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Simple pattern matching negation
Message-Id: <Xns96E4EA6149BFBasu1cornelledu@127.0.0.1>

Todd de Gruyl <todd@tdegruyl.com> wrote in 
news:slrndk3rs4.1uj.todd@ibook.local:

> On 2005-10-04, Bob <uctraing@ultranet.com> wrote:
>> This works (checking for 4 numeric digits and doing something if
>> it's _not- true:
> 
> I'm assuming you mean 5 digits.
> 
>>      if ($zip5=~m/[0-9]{5}/ != 1) { print ("invalid zip code");}
>>
>> but I know there must be an easier syntax to negate the expression
>> $zip5=~m/[0-9]{5}/ within the if statement. I tried a couple of 
>> things that I thought should work but I am missing something really
>> basic in the use of "not" or "!". 
> 
> if($zip5 !~ /[0-9]{5}/){
>      print "invalid zip code";
> }
> 
> or (depending on your needs)
>      print "invalid zip code" unless $zip5 =~ /\d{5}/;
> or:
>      print "invalid zip code" if $zip5 !~ /\d{5}/;

Assuming that the OP wants $zip5 to contain nothing but the 5 digits, 
you should use anchors:

print "invalid zip code\n" if $zip5 !~ /^\d{5}$/;

Sinan

-- 
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html


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

Date: Mon, 03 Oct 2005 21:05:22 -0600
From: Scott Bryce <sbryce@scottbryce.com>
Subject: Re: Simple pattern matching negation
Message-Id: <M7ydnfFG9t3sbNzeRVn-iA@comcast.com>

Bob wrote:

> but I know there must be an easier syntax to negate the expression
> $zip5=~m/[0-9]{5}/

if ($zip5 !~ /\d{5}/)

But I have a feeling that that isn't what you really want to do.

use strict;
use warnings;

my @zips = qw(12345 1234 ab12345 abcdefg 123456);

for my $zip5 (@zips)
{
	if ($zip5 !~ /\d{5}/)
	{
		print "$zip5 does not match (1).\n";
	}
	else
	{
		print "$zip5 does match (1).\n";
	}
	
	if ((length($zip5) != 5) || ($zip5 =~ /\D/))
	{
		print "$zip5 does not match (2).\n";
	}
	else
	{
		print "$zip5 does match (2).\n";
	}
	
	if ($zip5 !~ /^\d{5}$/)
	{
		print "$zip5 does not match (3).\n";
	}
	else
	{
		print "$zip5 does match (3).\n";
	}
	
	print "\n";
}

You may also want to account for zip + 4 or postal codes from outside 
the USA.


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

Date: Tue, 4 Oct 2005 00:18:57 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Simple pattern matching negation
Message-Id: <slrndk4461.l9u.tadmc@magna.augustmail.com>

Bob <uctraing@ultranet.com> wrote:

> This works 


Completely by accident.

It could stop working the next time you upgrade perl!


> 	if ($zip5=~m/[0-9]{5}/ != 1) { print ("invalid zip code");}
                               ^^^^
                               ^^^^
The value of a match in scalar context is true or false. Relying
on it being any particular true value is asking for a bug.


> but I know there must be an easier syntax to negate the expression
> $zip5=~m/[0-9]{5}/ within the if statement. 


The basic idiom for validating data is: anchor the front, anchor
the back, in between put a pattern that accounts for all you
want to allow:

   if ( $zip5 !~ m/^\d{5}$/ ) { print "invalid zip code"}
or
   unless ( $zip5 =~ m/^\d{5}$/ ) { print "invalid zip code"}


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 8481
***************************************


home help back first fref pref prev next nref lref last post