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 do I send output of print statement to array

 



regex2012
User

Feb 17, 2016, 12:59 PM

Post #1 of 7 (1610 views)
How do I send output of print statement to array Can't Post

I put together this duplicate identifier:



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


I am trying to get the output of the print command into an array instead of a file, so I can work with it - does anyone know how? I get the results I want but don't know how to store it other than writing it to a new file.


FishMonger
Veteran / Moderator

Feb 17, 2016, 1:41 PM

Post #2 of 7 (1608 views)
Re: [regex2012] How do I send output of print statement to array [In reply to] Can't Post

Is this what you're wanting?

Code
push @array, $_ if defined $duplicates{$_};



regex2012
User

Feb 17, 2016, 3:20 PM

Post #3 of 7 (1603 views)
Re: [FishMonger] How do I send output of print statement to array [In reply to] Can't Post

That gives the entire list, and not what prints when the

Code
print if defined $duplicates{$_};

executes.

I am not sure why it does not work in the same way
Why wouldn't

Code
push my @array, $_ if defined $duplicates{$_};

show the same thing as

Code
print if defined $duplicates{$_};

when I use

Code
print "@array";



Chris Charley
User

Feb 17, 2016, 4:00 PM

Post #4 of 7 (1597 views)
Re: [regex2012] How do I send output of print statement to array [In reply to] Can't Post

Your print is printing lines that have been seen before. Would you like an array containing ('a line', 'another line', 'a line', 'a line', 'seen before', 'another line')?

I think that you should get the same results by pushing to an array as the print statement.

push my @array, $_ if defined $duplicates{$_};
print "@array";


Should be equal to

print if defined $duplicates{$_};


(This post was edited by Chris Charley on Feb 17, 2016, 4:06 PM)


FishMonger
Veteran / Moderator

Feb 17, 2016, 7:17 PM

Post #5 of 7 (1587 views)
Re: [regex2012] How do I send output of print statement to array [In reply to] Can't Post


Code
#!/usr/bin/perl 

use warnings;
use strict;
use Data::Dumper;

my (%seen, @dups);
while (<DATA>) {
chomp;
push @dups, $_ if $seen{$_}++;
}
print Dumper \@dups;

__DATA__
apple
pear
apple
grape
banana
pear



Code
c:\test>Perl-1.pl 
$VAR1 = [
'apple',
'pear'
];



Laurent_R
Veteran / Moderator

Feb 18, 2016, 2:09 AM

Post #6 of 7 (1578 views)
Re: [regex2012] How do I send output of print statement to array [In reply to] Can't Post

Another way to look at it is to say that at the end of the while loop in your original code, the %duplicates hash contains everything you need to know, including the number of occurrence of each item.

So if you just want a list of unique duplicates, it could be:


Code
my @dupes = grep {$duplicates{$_} > 1} keys %duplicates;

which keeps every item which has been seen more than once in the loop (the grep instruction just filters out the items that exist only once).

Or if you want your array to store how many times each duplicate has been seen:

Code
my @dupes = map { [$_, $duplicates{$_}] }  grep {$duplicates{$_} > 1} keys %duplicates;

Or if you want to display the duplicates:

Code
print "$_ : $duplicates{$_}\n" for  grep {$duplicates{$_} > 1} keys %duplicates;



regex2012
User

Feb 19, 2016, 4:10 PM

Post #7 of 7 (1544 views)
Re: [FishMonger] How do I send output of print statement to array [In reply to] Can't Post

The data dumper really works well! I will read more about it on the Perl Docs. Thanks!

 
 


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

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