Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Loop Through Files and Update Headers

by jalopez453 (Acolyte)
on Mar 05, 2020 at 21:15 UTC ( [id://11113874]=perlquestion: print w/replies, xml ) Need Help??

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

I have a script where I am looping through all files in the folder and I am wanting to update the headers. The issue I am having is, it is looping through all the files and appending the new header but it is not copying down the remaining data in each file, it is only pasting the header. Can someone take a look at my code and tell me what is missing to append the remaining data in my files and not just the header.

use strict; use warnings; use Tie::File; opendir IN, 'Output'; # name of folder where files are my @in = grep { /\.txt$/ } readdir IN; # read all files in dir closedir IN; for my $in (@in) { open IN, '<', "Output/$in" || next; tie my @array, 'Tie::File', $in or die "Could not open file '$in' $!"; $array[0] = 'Header1, Header2, Header3, etc etc.'; untie @array; }

Replies are listed 'Best First'.
Re: Loop Through Files and Update Headers
by choroba (Cardinal) on Mar 05, 2020 at 22:54 UTC
    Why do you open the file using open? You don't seem to be using the file handle IN anywhere.

    But, when opening it, you correctly specify the path as "Output/$in". You should do the same when tying the file:

    tie my @array, 'Tie::File', "Output/$in" or die ... # ~~~~~~~

    Otherwise, it just creates a new file in the current directory, populates its first line, and that's it.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: Loop Through Files and Update Headers
by kcott (Archbishop) on Mar 06, 2020 at 06:58 UTC

    G'day jalopez453,

    As has already been mentioned, you've some problems in your code. Another, is "untie @array" inside the for loop: indenting your code probably would have made that more obvious.

    If you have a different number of new headers than what was there originally, consider using splice. Here's a quick example:

    $ perl -E ' my @x = qw{H-old ...}; say for @x; splice @x, 0, 1, qw{H-new1 H-new2}; say for @x; ' H-old ... H-new1 H-new2 ...

    — Ken

Re: Loop Through Files and Update Headers
by choroba (Cardinal) on Mar 05, 2020 at 23:24 UTC
    Crossposted to reddit. Next time, please, inform about crossposting to avoid unnecessary work of hackers not attending both the sites.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: Loop Through Files and Update Headers
by Marshall (Canon) on Mar 07, 2020 at 03:32 UTC
    I would not use Tie::File unless there is some user requirement of which I am not aware.
    I would read the input file into memory, save input file as a backup, modify input file as in memory, write modified contents to the output file.
    Untested, but hopefully gets you on the right track...
    use strict; use warnings; my $inDir ="."; my @inFiles = glob ("$inDir/*.txt"}; foreach my $infile (@inFiles) { # read infile into memory.. open IN, '<', "$inDir/$infile" or die "$!"; my @lines = <$infile>; close IN; # save the original infile rename ("$inDir/$infile", "$inDir/$infile-backup") or die "$!"; # modify the data that's in memory unshift @lines, "whatever_you_want\n"; #save the new data into the original file name open (OUT, '>', "$inDir/$infile") or die "$!"; print OUT @lines; close OUT; }
    There are issues when "something goes wrong" and Tie::File. The above saves a "backup" in the case of problems.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-03-29 07:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found