Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Seeing double

by indigo (Scribe)
on Apr 20, 2001 at 01:35 UTC ( #74005=note: print w/replies, xml ) Need Help??


in reply to Seeing double

Sure.
#!/usr/bin/perl -w use strict;
Always use -w and use strict. my $textfile = "iwn.txt"; Use my to declare variables under use strict.
open TEXT, $textfile or die "Can't Open $textfile"; my ($email, $id); while (<TEXT>){ $email ||= $_ if /Email address:/; $id ||= $_ if /Message-Id:/; }
You were using C style looping. Perl loops are much nicer.

$x ||= $y (or equals) is the same as $x = $x || $y. The end effect for you is $id gets a value only if it doesn't already have one.
close TEXT; print $email; print $id;
Pulling these variables out of the loop ensures they only get printed once.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://74005]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2020-10-24 04:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (242 votes). Check out past polls.

    Notices?