0

我正在尝试打印符合我的特定条件的设备列表。当我将所有内容打印到屏幕上时,效果很好。但是,当我将它打印到文件时,它只打印一行。我是 perl 的新手,所以任何帮助将不胜感激。谢谢

$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);
}
4

1 回答 1

6

您正在overwrite.txt为每个$data项目写一次。每次都会被覆盖。将其移出循环。

于 2013-06-29T22:43:22.317 回答