Rodrick Brown wrote:

I have a data file with the following about a thousand or so records.

=============== XXXXXXXXXX01 ===============
0
xxxxxxxxx01


I performing the following match to just pick out the xxxx strings

while(<DATA>)
{
 if( $_ =~ m/^=+\s(\w+)/ )
 {
   $hostname = lc $1;
 }
 print $hostname . "\n";
}

some odd reason my data is returned 3x instead of once why does this happen?

Hi Rodrick

Because $hostname is printed for every line in the data, even though it is
modified only when the pattern matches.

Putting

use strict;
use warnings;

at the head of your program would help, together with declaring your
variables at the smallest possible scope. Try something like the program
below to see what I mean.

HTH,

Rob


use strict;
use warnings;

while(<DATA>) {

 my $hostname = 'not found';

 if( $_ =~ m/^=+\s(\w+)/ ) {
   $hostname = lc $1;
 }

 print $hostname . "\n";
}


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to