Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Perl prints only one line when directed to a file but print to a screen works

by vlad3848 (Acolyte)
on Jun 30, 2013 at 17:10 UTC ( [id://1041621]=perlquestion: print w/replies, xml ) Need Help??

vlad3848 has asked for the wisdom of the Perl Monks concerning the following question:

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

open OVERWRITE, ">overwrite.txt" or die $! $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 > primaryIPAddr +ess; $deviceHash{"hostName"} = $listDevices->hostName; # mapping array my @returnData = ( "deviceID", "hostName", "primaryIPAddress", "deviceType", "vendor", "model" ); # 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} . " +,"; } } } &closeConnection($dbConnection); } close OVERWRITE;
  • Comment on Perl prints only one line when directed to a file but print to a screen works
  • Download Code

Replies are listed 'Best First'.
Re: Perl prints only one line when directed to a file but print to a screen works
by Happy-the-monk (Canon) on Jun 30, 2013 at 17:17 UTC

    foreach my $listDevices ($device_list->result()) { ... open OVERWRITE, ">overwrite.txt" or die $! ... close OVERWRITE; ... }

    Do you see what is happening?

    You have even called the filehandle "OVERWRITE".

    The file gets overwritten every time the loop and if directive come by the call to open

    Solution: place both the open and close outside of the foreach loop.

    open goes first, then the loop, and when that's done, close the filehandle.

    Cheers, Sören

    (hooked on the Perl Programming language)

      Thank you Sören!
Re: Perl prints only one line when directed to a file but print to a screen works
by hdb (Monsignor) on Jun 30, 2013 at 17:17 UTC

    As you overwrite your file for every iteration of your outer loop, you might want to either open it outside the loop or open it for appending (in contrast to the name...).

      That solved it! Thank you very much!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-19 03:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found