Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

After i made the script of commenting out lines , it starts to make bugs :S <<HELP!>>

by firewall00 (Acolyte)
on Oct 07, 2007 at 16:51 UTC ( #643295=perlquestion: print w/ replies, xml ) Need Help??
firewall00 has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks
finally i made that code which is really a code, to comment out the named.conf file
by choosing the domain name and then it will make matching and provide a counter, which is actually to put the comment from the matched line and till 5 five line starting form this matched line
so here is the code
#!/usr/bin/perl -w use strict; print " please enter the domain name: "; my $targetdomain =<STDIN> ; chomp $targetdomain; my $file = "/home/adam/Desktop/hello"; open HAN,$file || die "error opening file: $!"; my @array =<HAN> ; close(HAN); foreach my $x ( 0 .. $#array ){ if( $array[$x] =~ m/^zone\s+"$targetdomain"/ ){ for my $n (0 .. 4) { $array[$x+$n] = "//" . $array[$x+$n]; } } } open HAN, ">$file"; print HAN "@array"; close(HAN);
it works fine for the first time but i noticed somthing which make me confused
every time i run this script on the file the file it shifted one space from the left like that
this is the status of the file before running the script .
zone "domain.com" type master; file "/path/to/zone/domain.com"; notify yes; }; zone "mydomain.com" { type master file "/path/to/zone/mydomain.com"; notify yes }; zone "do.com" { type master; file "/path/to/zone/do.com"; notify yes; };
and after running it
zone "domain.com" type master; file "/path/to/zone/domain.com"; notify yes; }; //zone "mydomain.com" { //type master //file "/path/to/zone/mydomain.com"; //notify yes //}; zone "do.com" { type master; file "/path/to/zone/do.com"; notify yes; };
the script comment out the zone as i nedded but, you see the space added the left of the file and after run it again the spac is increased by one and so on . .
how can i prevent such space. that is must for my code to keep the file without any changes and without any spaces . and just comment the nedded zones .
thanks MonKs

Comment on After i made the script of commenting out lines , it starts to make bugs :S <<HELP!>>
Select or Download Code
Re: After i made the script of commenting out lines , it starts to make bugs :S <<HELP!>>
by Corion (Pope) on Oct 07, 2007 at 16:58 UTC

    What have you done so far to debug this problem?

    The spaces come because you do:

    print HAN "@array";

    And that's just what Perl does. When you print an array, Perl inserts $" between the array elements. And $" is set to a space by default. Read perlvar and maybe do:

    local $" = "";

    before printing. Or print line by line:

    print HAN $_ for @array;

    @array is a very bad name for a list containing the lines of a file.

Re: After i made the script of commenting out lines , it starts to make bugs :S <<HELP!>>
by strat (Canon) on Oct 08, 2007 at 10:25 UTC
    open HAN,$file || die "error opening file: $!";

    This doesn't do what you expect it to do because || binds stronger than , and so you are executing something like open HAN, ( $file || die ... )

    Better use one of the following ways (I recommend the first way):

    open( HAN, $file ) or die "error opening file: $!"; open( HAN, $file ) || die "error opening file: $!"; open HAN, $file or die "error opening file: $!";

    Best regards,
    perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

      Thanks for you Tip

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2014-11-23 11:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (131 votes), past polls