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:
print if defined statement to file

 



regex2012
User

Apr 19, 2016, 9:23 AM

Post #1 of 3 (1327 views)
print if defined statement to file Can't Post

I am trying to print this to a file:

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

but I haven't been able to . I am not sure where to indicate the file handle in this statement to get it to print the duplicate line to /tmp/tst.txt.

Can anyone help?


(This post was edited by regex2012 on Apr 19, 2016, 9:24 AM)


BillKSmith
Veteran

Apr 19, 2016, 10:40 AM

Post #2 of 3 (1322 views)
Re: [regex2012] print if defined statement to file [In reply to] Can't Post


Code
print {$FH6} $_ if defined $duplicates{$_};


Note: The braces are not required, but they make the statement easier to read because emphasize the difference between the filehandle and the data.
Good Luck,
Bill


FishMonger
Veteran / Moderator

Apr 19, 2016, 12:01 PM

Post #3 of 3 (1317 views)
Re: [BillKSmith] print if defined statement to file [In reply to] Can't Post

It should be noted that the braces would be required if the filehandle was not a plain scalar.

# braces not requried

Code
print {$FH6} $_ if defined $duplicates{$_};


# braces are required

Code
print { $FH[6] } $_ if defined $duplicates{$_};


 
 


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

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