3

I am getting an output from a subroutine as

 @ outputarray

outputarray[0]=name  ip  port 
outputarray[1]=------------------------------------ 
outputarray[2]=http-listener-1  *   6712
outputarray[3]=http-listener-2  *   4743
...... etc

I want to create a new file and write to file log.txt where file content looks as below and delete the file log.txt how can I achieve this in perl?

 name ip port 
------------------------------------ 
http-listener-1 *      6712 
http-listener-2 *      4743

Thanks

4
  • pls. post your so far code. Commented Jan 18, 2013 at 10:34
  • 1
    You want to write to log.txt and then delete log.txt? Sounds redundant, don't you think? Commented Jan 18, 2013 at 10:35
  • Yes I have one more subroutine to scan the port I will delete it after that Commented Jan 18, 2013 at 10:38
  • I assume that your number 4743 changing to 8709 in the output is just a typo? Commented Jan 18, 2013 at 10:39

1 Answer 1

8

How to open a file and print an array to it:

open my $fh, ">", "log.txt" or die $!;
print $fh "$_\n" for @outputarray;
close $fh;

How to delete that file:

unlink "log.txt" or die $!;

Why you would want to first print and then delete that file, I have no idea.

Sign up to request clarification or add additional context in comments.

4 Comments

Sorry i am newbie to perl does your first line also creates new file?
> is the open mode that creates a new file, yes. And it also overwrites any old file with that name.
where can I get more information about the syntax used in the print statement? It makes sense to me, but as a perl newbie, I didn't know you could use it that way in a print statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.