not /pattern/

Discussion in 'Perl Misc' started by MattJ83, Sep 13, 2006.

  1. MattJ83

    MattJ83 Guest

    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
     
    MattJ83, Sep 13, 2006
    #1
    1. Advertising

  2. MattJ83 <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Sep 13, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    17
    Views:
    1,890
    Chris Uppal
    Nov 16, 2005
  2. sunny
    Replies:
    1
    Views:
    471
    Salt_Peter
    Dec 7, 2006
  3. Pallav singh
    Replies:
    0
    Views:
    373
    Pallav singh
    Jan 22, 2012
  4. Pallav singh
    Replies:
    0
    Views:
    410
    Pallav singh
    Jan 22, 2012
  5. Pallav singh
    Replies:
    1
    Views:
    460
    Peter Remmers
    Jan 22, 2012
Loading...

Share This Page