Home: Perl Programming Help: Beginner:
how to save output (not file handles or to screen)


Feb 25, 2016, 8:11 AM

Views: 2168
how to save output (not file handles or to screen)

One of the biggest problems I am having is knowing how to save output to an array and not either print it on a screen or send it to a file.
Here's an example:

use strict;

open my $FH1, '<', '/tmp/active_qz.txt' or die "unable to open file 'file' for reading : $!";
my %seen;
while ( <$FH1> ) {
print if $seen{$_}++;

This prints just fine the duplicate line but I haven't figured out how to save it to an array or how that syntax works. If I just print it, I can't put use it any where else. I can print it to a file, but it is messy to generate lots of files. Can anyone recommend a solution for this script snippet and also for general study for other similar situations?

Veteran / Moderator

Feb 25, 2016, 9:16 AM

Views: 2163
Re: [regex2012] how to save output (not file handles or to screen)

How does the answer I and Laurent gave you in your "How do I send output of print statement to array" thread fail to do what you're asking in this one?

(This post was edited by FishMonger on Feb 25, 2016, 9:18 AM)


Feb 25, 2016, 8:20 PM

Views: 2147
Re: [regex2012] how to save output (not file handles or to screen)

This is the third time you have asked almost the same question. You have received several good answers. Most of them involve replacing print with push or other functions. You have not explained why this does not meet your requirements. Perhaps, you really want to capture the output of the print.

The output of print is a stream of characters. This stream can be directed to a file, an external device, another program, or a string using options in the open statement. It is not possible to direct it to an array. Perhaps you are thinking of the C language where a 'string' is an array of characters. Refer to the section of

perldoc perlfunc -f open

Related to 'In-memory' files for information on opening a filehandle which will "print' to a perl string.
Good Luck,

Veteran / Moderator

Feb 25, 2016, 11:07 PM

Views: 2141
Re: [regex2012] how to save output (not file handles or to screen)

Yeah, since you've already been given answers to almost exactly the same question, perhaps you should clarify what you really need if these answers did not fit your requirements.