Perl只打印一行,但打印到屏幕上是可行的

Perl只打印一行,但打印到屏幕上是可行的,perl,perl-module,mod-perl,perl-data-structures,Perl,Perl Module,Mod Perl,Perl Data Structures,我正在尝试打印符合我的特定条件的设备列表。当我将所有内容打印到屏幕上时,效果非常好。但是,当我将其打印到文件时,它只打印一行。我是perl新手,希望您能给予我帮助。谢谢 $dbConnection = &openConnection(); # run the "list_device" command via the initial connection my $device_list = $dbConnection->list_device(); foreach my $list

我正在尝试打印符合我的特定条件的设备列表。当我将所有内容打印到屏幕上时,效果非常好。但是,当我将其打印到文件时,它只打印一行。我是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);
}

您正在打开
overwrite.txt
,以便为每个
$data
项写入一次。每次都会被覆盖。将其移出循环。

您正在打开
overwrite.txt
,以便每个
$data
项写入一次。每次都会被覆盖。将其移出循环。

由于在
open
中有奇怪的引用,该代码甚至不应该编译。请注意,
“hostname”
(数组条目)和
“hostname”
(散列键)不相同,并且
$i<@returnData
始终为真,因为您在前一行中将
$i
设置为零。这也是完全不必要的,因为循环将执行
scalar@returnData
次,因此即使
$i
是实际的数组索引,条件也将始终保持不变。Amon,感谢您的评论。这是我第一次在这里发帖,我很难在评论框中复制和粘贴我的代码。我是手工输入的,一定是输入错了“主机名”。由于
open
中的奇怪引用,我确实使用了“$iThat”代码,它甚至不应该编译。请注意,
“hostname”
(数组条目)和
“hostname”
(散列键)不相同,并且
$i<@returnData
始终为真,因为您在前一行中将
$i
设置为零。这也是完全不必要的,因为循环将执行
scalar@returnData
次,因此即使
$i
是实际的数组索引,条件也将始终保持不变。Amon,感谢您的评论。这是我第一次在这里发帖,我很难在评论框中复制和粘贴我的代码。我是手工输入的,一定是输入错了“主机名”。我确实使用了$iDaxim,非常感谢您的评论!我已将oppening语句移到循环之外:my@returnData=(“deviceID”、“hostName”、“PrimaryPaddress”、“deviceType”、“vendor”、“model”);打开OVERWRITE,“>OVERWRITE.txt”或die$!;foreach my$data(@returnData){if(exists$deviceHash{$data}){print OVERWRITE$deviceHash{$data}.”、“;}}}关闭覆盖;然而,它仍然打印一个line@user2535431-您需要将其移动到两个循环之外。或者,您可以在附加模式下打开文件:
openoverwrite,'>>,'OVERWRITE.txt'
。一般你也应该大喜,非常感谢你的评论!我已将oppening语句移到循环之外:my@returnData=(“deviceID”、“hostName”、“PrimaryPaddress”、“deviceType”、“vendor”、“model”);打开OVERWRITE,“>OVERWRITE.txt”或die$!;foreach my$data(@returnData){if(exists$deviceHash{$data}){print OVERWRITE$deviceHash{$data}.”、“;}}}关闭覆盖;然而,它仍然打印一个line@user2535431-您需要将其移动到两个循环之外。或者,您可以在附加模式下打开文件:
openoverwrite,'>>,'OVERWRITE.txt'
。一般来说,你也应该