Printing only on a match

D

Diamond, Mark

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
 
J

Jens Thoms Toerring

Diamond said:
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.
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
 
B

Bart Lateur

dee said:
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";
}
 
J

Jürgen Exner

Diamond said:
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
 
D

Diamond, Mark

Many thanks to all. My perl code now works correctly and is much easier to
read.

Cheers,
Mark
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top