Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Copying files from one file to another file

by davido (Cardinal)
on Feb 19, 2018 at 20:14 UTC ( [id://1209505]=note: print w/replies, xml ) Need Help??


in reply to Copying files from one file to another file

I'm assuming that the "a[0] -", "a[1] -", and "a[2] -" prefixes on each line of your sample input are not part of the contents of your array, but rather, your demonstration of what element index of the array is being represented, and that the part that comes after that is the contents of your array. Also you were unclear in your explanation on what is supposed to happen with the line number portion of the string contained in each element of the array. Because it was unclear, I'm leaving it out of my demonstrated solution.

use strict; use warnings; my @array = <DATA>; foreach my $element (@array) { chomp $element; my ($fname, $line_number, $content) = split /\s*:\s*/, $element; open my $outfh, '>>', $fname or die "Cannot open $fname: $!\n"; print $outfh $content; # You didn't tell us what is supposed to ha +ppen with $line_number. close $outfh or die "Couldn't close $fname after writing: $!\n"; } __DATA__ tests/right/case1: 12 : //comment tests/right/case1: 13 : //comment test/right/case3: 5 : //comment

The standard behavior of the  >> open mode is to create if the file does not exist, and append if it does, so you really don't need to worry about whether you've seen the file before.

If the code in this answer is difficult to follow I encourage you to spend some time with the following Perl documentation, which comes with any full installation of Perl.


Dave

Replies are listed 'Best First'.
Re^2: Copying files from one file to another file
by harishnv (Sexton) on Feb 20, 2018 at 07:44 UTC
    If the path is same, both the lines should be saved in one file and in different files.
      If the path is same, both the lines should be saved in one file and in different files. [emphasis added]

      I don't understand this statement.

      Update: I also don't understand why is not possible to provide an exact example of an input file you wish to parse (in  <code> tags), less than a dozen lines would seem enough, and also exact examples of the output files produced from the example input file (again, in  <code> tags). You've already done the first (maybe — see davido's comments here), so the output files would not seem a much greater step. Please see this. Having exact examples of both input and output might go a long way toward clarifying the situation.


      Give a man a fish:  <%-{-{-{-<

        I have commented below everything, please check and help.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (9)
As of 2024-04-18 16:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found