CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



regex2012
User

Feb 25, 2016, 8:11 AM

Post #1 of 4 (1520 views)
how to save output (not file handles or to screen) Can't Post

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;
#!/usr/bin/perl

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

Code
 
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?


FishMonger
Veteran / Moderator

Feb 25, 2016, 9:16 AM

Post #2 of 4 (1515 views)
Re: [regex2012] how to save output (not file handles or to screen) [In reply to] Can't Post

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)


BillKSmith
Veteran

Feb 25, 2016, 8:20 PM

Post #3 of 4 (1499 views)
Re: [regex2012] how to save output (not file handles or to screen) [In reply to] Can't Post

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

Code
perldoc perlfunc -f open

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


Laurent_R
Veteran / Moderator

Feb 25, 2016, 11:07 PM

Post #4 of 4 (1493 views)
Re: [regex2012] how to save output (not file handles or to screen) [In reply to] Can't Post

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.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives