formating output

Hi all, I want to start a new topic on this matter

I have this script,

#!perl
use strict; 
use warnings; 
use Data::Dumper; 
 
open my $log, '>', 'log-external.txt' or die "Could not open log: $!"; 
print $log "Subnet,Static,DHCP,Unused\n"; 
 
open my $dump, '>', 'dump.log' or die "failed to open 'dump.log' $!"; 
 
##### Step 1, read subnets 
open my $in, '<', 'm-names.txt' or die "Could not open m-names.txt: $!\n"; 
while( my $subnet = <$in>) { 
 
    print "Checking $subnet"; 
    chomp $subnet; 
     
    my %counts = (  
                   Static => 0, 
                   DHCP   => 0, 
                   Unused => 0, 
    ); 
     
    my @dnsoptions = `./getobjectlst.exe -u xx -p xx -o rich -a $subnet 2>&1`;
 
    print $dump "dumping \@dnsoptions\n";     
    print $dump Data::Dumper->Dump(\@dnsoptions); 
    print $dump '=' x 25, "\n"; 
 
    if ( @dnsoptions and $dnsoptions[0] eq '' ) {
        print "dnsoptions is null or undefined, going on to the next subnet\n"; 
        next; 
    } 
 
    # Now, at this point, we may have "Error 48" in @dnsoptions, or we may have 
    # the nicely formatted output. We'll have to check for both cases here. 
    # Let's check the unsuccessful case first. The condition below checks if 
    # the first element of @dnsoptions array has the following text in it - 
    # "Error 48: This subnet does not exist." in it. 
 
 
    if (join("",@dnsoptions) =~ /Error 48: This subnet does not exist./) { 
 
        # call "getsubnetlst.exe", passing $subnet as one of the parameters 
        my @subnetpart2 = `./getsubnetlst.exe -u xx-p xx -o rich -a $subnet -t network`; 
 
        print $dump "dumping \@subnetpart2\n";     
        print $dump Data::Dumper->Dump(\@subnetpart2); 
        print $dump '=' x 25, "\n"; 
 
         
        # now loop through each element of the array @subnetpart2, which looks like this - 
        # ########################################################################## 
        # "East" "146.149.1.0" "N" "" "146.149.0.0" " " " " "255.255.255.128" 
        # ########################################################################## 
        # pick up the 2nd field from the left (e.g. 146.149.1.0 above), and pass it as 
        # a parameter to the cli "getobjectlst.exe". 
 
 
        foreach my $line ( @subnetpart2 ) { 
 
            # get the 2nd field from the left 
            my $snetpart2 = (split/"\s+"/, $line)[1];
             if( $snetpart2 =~ /"/ ){
             warn "\$snetpart2 was $snetpart2\n when \$line was $line";
             next;  
} 
            # and now pass it to "getobjectlst.exe"; 
            my @dnsoptions2 = `./getobjectlst.exe -u xx -p xx -o rich -a $snetpart2`;
 
            print $dump "dumping \@dnsoptions2\n";     
            print $dump Data::Dumper->Dump(\@dnsoptions2); 
            print $dump '=' x 25, "\n"; 
             
            # find out counts of each subnettype (4th field from left) 
            foreach my $line (@dnsoptions2) { 
                my @subnettype = split/"\s+"/, $line; 
                $counts{$subnettype[3]}++; 
            } 
        } 
    } 
    else { # successful output from getobjectlst.exe 
 
        # find out counts of each subnettype (4th field from left) 
        foreach my $line (@dnsoptions) { 
            my @subnettype = split/"\s+"/, $line;
            $counts{$subnettype[3]}++; 
        } 
    } 
    printf $log "%s,%d,%d,%d\n", $subnet, $counts{Static}, $counts{DHCP}, $counts{Unused}; 
} 
 
close($in);close($log); 

How can I format the results, seems like its doing a carriage return.

Now it looks like this:

Subnet,Static,DHCP,Unused

10.0.0.0/8
,37,0,217
146.149.0.0/16
,174,0,39314

I would like it formatted like so,

Subnet,Static,DHCP,Unused

10.0.0.0/8,37,0,217
146.149.0.0/16,174,0,39314

Thanks

I didn't trace through your code, but this usually happens when you assign a value to a variable that contains a \n in it. You can get around this by using the chomp() function to strip out any newlines from strings.

Using the Perl chomp() function

HTH

Thanks !