Printing only on a match

Discussion in 'Perl Misc' started by Diamond, Mark, Aug 24, 2009.

  1. This post is prompted by having my earlier mistake ("Preventing lines from
    printing 23/8/09) pointed out to me. My problem is now one of elegance
    rather than failure.

    I have

    while (<>) {
    ..
    ..
    ($angle, $stage)= /^(\d\d)(\d+)/;
    print "$angle $stage\n";
    ..
    ..
    }

    which prints something whether there is a match or not. I assume that I can
    avoid the printing when there is no match by comparing $angle and $stage
    with null strings, but please, what is the/a sensible/elegant way of
    printing on a complete match and not printing on no match or only a partial
    match?

    Cheers,
    Mark
    Diamond, Mark, Aug 24, 2009
    #1
    1. Advertising

  2. Diamond, Mark <> wrote:
    > This post is prompted by having my earlier mistake ("Preventing lines from
    > printing 23/8/09) pointed out to me. My problem is now one of elegance
    > rather than failure.


    > I have


    > while (<>) {
    > .
    > .
    > ($angle, $stage)= /^(\d\d)(\d+)/;
    > print "$angle $stage\n";
    > .
    > .
    > }


    > which prints something whether there is a match or not. I assume that I can
    > avoid the printing when there is no match by comparing $angle and $stage
    > with null strings,


    i guess you meant by checking if they are defined.

    > but please, what is the/a sensible/elegant way of
    > printing on a complete match and not printing on no match or only a partial
    > match?


    The simplest way might be

    while (<>) {
    print "$1 $2\n" if /^(\d{2})(\d+)/;
    }

    Don't know if it sensible/elegant enough for your eyes;-)

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, Aug 24, 2009
    #2
    1. Advertising

  3. Diamond, Mark

    Bart Lateur Guest

    dee wrote:

    >Often, but not always, a sensible way is to add "or next":
    >
    >while (<>) {
    >.
    >.
    > ($angle, $stage)= /^(\d\d)(\d+)/ or next;
    > print "$angle $stage\n";
    >.
    >.
    >}
    >
    >This doesn't work outside of a loop, but these situations very typically
    >appear in loops, so I use it often.


    For the general case, you can use the slightly more verbose:

    if( my($angle, $stage)= /^(\d\d)(\d+)/ ) {
    print "$angle $stage\n";
    }


    --
    Bart.
    Bart Lateur, Aug 24, 2009
    #3
  4. "Diamond, Mark" <> wrote:
    >This post is prompted by having my earlier mistake ("Preventing lines from
    >printing 23/8/09) pointed out to me. My problem is now one of elegance
    >rather than failure.
    >
    >I have
    >
    >while (<>) {
    >.
    >.
    > ($angle, $stage)= /^(\d\d)(\d+)/;
    > print "$angle $stage\n";
    >.
    >.
    >}
    >
    >which prints something whether there is a match or not. I assume that I can
    >avoid the printing when there is no match by comparing $angle and $stage
    >with null strings, but please, what is the/a sensible/elegant way of
    >printing on a complete match and not printing on no match or only a partial
    >match?


    Per idiom for that kind of task:

    print "$angle $stage\n" if ($angle, $stage)= /^(\d\d)(\d+)/;

    jue
    Jürgen Exner, Aug 24, 2009
    #4
  5. Many thanks to all. My perl code now works correctly and is much easier to
    read.

    Cheers,
    Mark
    Diamond, Mark, Aug 25, 2009
    #5
    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. hiwa
    Replies:
    0
    Views:
    628
  2. Victor
    Replies:
    2
    Views:
    627
    Victor
    May 17, 2004
  3. ekzept
    Replies:
    0
    Views:
    354
    ekzept
    Aug 10, 2007
  4. John Gordon
    Replies:
    13
    Views:
    457
    Ian Kelly
    Dec 20, 2011
  5. Volkan Civelek

    Match doesn't match

    Volkan Civelek, Jul 19, 2006, in forum: Ruby
    Replies:
    4
    Views:
    154
Loading...

Share This Page