Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Printing only on a match

Reply
Thread Tools

Printing only on a match

 
 
Diamond, Mark
Guest
Posts: n/a
 
      08-24-2009
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



 
Reply With Quote
 
 
 
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      08-24-2009
Diamond, Mark <(E-Mail Removed)> 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://www.velocityreviews.com/forums/(E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
 
 
 
Bart Lateur
Guest
Posts: n/a
 
      08-24-2009
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.
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      08-24-2009
"Diamond, Mark" <(E-Mail Removed)> 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
 
Reply With Quote
 
Diamond, Mark
Guest
Posts: n/a
 
      08-25-2009
Many thanks to all. My perl code now works correctly and is much easier to
read.

Cheers,
Mark


 
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
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 2 11-27-2010 12:12 PM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:52 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:50 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:28 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-18-2007 10:11 AM



Advertisments