0

I am trying to print a list of the devices that match my specific criteria. When I print everything to the screen, it works great. However, when I print it to a file it only prints one line. I am new to perl so any help would be appreciated. Thanks

$dbConnection = &openConnection();
# run the "list_device" command via the initial connection
my $device_list = $dbConnection->list_device();
foreach my $listDevices ($device_list->result()) {
    if (   ($listDevices->model !~ /PIX/)
        && ($listDevices->model !~ /ASA/)
        && ($listDevices->model !~ /ACE/)
        && ($listDevices->driverName !~ /Context/)
        && ($listDevices->hostName =~ /^ls1.*/i)
        && ($listDevices->vendor =~ /Cisco/)
    ) {
        #create device hash for LS
        $deviceHash{"deviceID"}         = $listDevices->deviceID;
        $deviceHash{"deviceType"}       = $listDevices->deviceType;
        $deviceHash{"vendor"}           = $listDevices->vendor;
        $deviceHash{"model"}            = $listDevices->model;
        $deviceHash{"primaryIPAddress"} = $listDevices > primaryIPAddress;
        $deviceHash{"hostName"}         = $listDevices->hostName;

        # mapping array
        my @returnData = (
            "deviceID",         "hostName",
            "primaryIPAddress", "deviceType",
            "vendor",           "model"
        );
        open OVERWRITE, ">overwrite.txt" or die $!            
        # loop through the hash and print out the device information
        foreach my $data (@returnData) {
            my $returnDataLength = @returnData;

            if (exists $deviceHash{$data}) {
                                print OVERWRITE $deviceHash{$data} . ",";
                                }
        }
        close OVERWRITE;
    }
    &closeConnection($dbConnection);
}
  • That code shouldn't even compile, due to weird quoting around `open`. Please notice that `"hostname"` (the array entry) and `"hostName"` (the hash key) are not the same, and that `$i < @returnData` will always be true, as you set `$i` to zero in the previous line. It is also completely unneccessary, as the loop will execute `scalar @returnData` times, so even if `$i` were the actual array index, the condition would always hold. – amon Jun 29 '13 at 22:47
  • Amon, thank you for your comment. This is my first time posting on here and i was having a hard time with copying and pasting my code inside the comment box. I typed that by hand and must have mistyped "hostname". I did take the '$i<@returnData' out, so thank you – user2535431 Jun 30 '13 at 15:52

1 Answers1

6

You are opening overwrite.txt for writing once per $data item. It gets overwritten each time. Move that outside of the loop.

daxim
  • 39,270
  • 4
  • 65
  • 132
  • Daxim, thank you very much for you comments! I've moved the oppening statement outside of the loop: my @returnData = ("deviceID","hostName","primaryIPAddress","deviceType", "vendor","model"); open OVERWRITE, ">overwrite.txt" or die $!; foreach my $data (@returnData) { if (exists $deviceHash{$data}) { print OVERWRITE $deviceHash{$data} . ","; } } close OVERWRITE; However, it still prints one line – user2535431 Jun 30 '13 at 15:45
  • @user2535431 - You need to move it outside of both loops. Alternatively, you can open the file in append mode: `open OVERWRITE, '>>', 'overwrite.txt'`. In general you should also [use the three argument open() command and avoid typeglobs for filehandles](http://stackoverflow.com/questions/1479741/why-is-three-argument-open-calls-with-autovivified-filehandles-a-perl-best-pract) – dms Jun 30 '13 at 17:47