Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > not /pattern/

Reply
Thread Tools

not /pattern/

 
 
MattJ83
Guest
Posts: n/a
 
      09-13-2006
Quick question
if i was to write code to match a /pattern/ but then wanted the script
to exit if it didn't find the pattern is it as simple as (!/pattern/) ?


Code:
{
if (/word/)
{
print "$_\n";
$info = $_;
} elsif /!word/ { exit; } # ????

Thanks

 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      09-13-2006
MattJ83 <(E-Mail Removed)> wrote:

> Quick question



It is a quick question that you could answer yourself by
writing a quick program to test your theory.


> if i was to write code to match a /pattern/ but then wanted the script
> to exit if it didn't find the pattern is it as simple as (!/pattern/) ?



What happened when you tried it?


> Code:
> {
> if (/word/)
> {
> print "$_\n";
> $info = $_;
> } elsif /!word/ { exit; } # ????

^^^^^^^

That is not Perl code. (and you put the ! in the wrong place)

This is Perl code:

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

$_ = 'this has a Word in it';

if (/word/) {
print "$_\n";
}
elsif ( ! /word/ ) {
print "here I would be calling exit()\n";
}
----------------------


See how easy that was?

No need to ask hundreds of people to help with something that simple...


But I would write it this way instead:

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

$_ = 'this has a Word in it';

exit unless /word/;
print "$_\n"; # must have matched for control to reach this statement
----------------------


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
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
To be not, or not to be not? Ruby Freak Ruby 2 09-23-2008 08:04 AM
Why not 'foo = not f' instead of 'foo = (not f or 1) and 0'? Kristian Domke Python 11 01-23-2008 07:27 PM
'' is not a valid name. Make sure that it does not include invalid characters or punctuation and that it is not too long. rote ASP .Net 2 01-23-2008 03:07 PM
Cisco 3640 3620 3600 not detecting, not enabling, not working: NM-2FE2W Taki Soho Cisco 0 09-22-2004 07:28 AM
maintaining control with cookies (not strictly an ASP or even server side question. But not not either) Stephanie Stowe ASP General 2 04-07-2004 04:23 PM



Advertisments