Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Getting duplicate file contents in perl

by noviceuser (Acolyte)
on Sep 13, 2021 at 12:32 UTC ( [id://11136712]=perlquestion: print w/replies, xml ) Need Help??

noviceuser has asked for the wisdom of the Perl Monks concerning the following question:

i am trying to read contents of all the file present in a folder and store/append all the file contents in a single text file, but i can see data getting repeated of same file in the output text file such as

suppose there are 3 files (1.txt, 2.txt, 3.txt) in the folder then the output file contains data from these files as below, i.e data of 1.txt gets repeated 3 times and data of 2.txt twice

<code> 1.txt 1.txt 2.txt 1.txt 2.txt 3.txt
  • Comment on Getting duplicate file contents in perl

Replies are listed 'Best First'.
Re: Getting duplicate file contents in perl
by BillKSmith (Monsignor) on Sep 13, 2021 at 13:38 UTC
    Your output file is already in the directory when you glob so your output file is treated as an input. Move the glob before the open.
    my @allFiles = glob("$severityDir/*.txt"); open( my $fh, '>>', $severityFile ) or die "Could not open file '$severityFile' $!";
    Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11136712]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (1)
As of 2024-04-25 01:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found