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


In reply to Re: Copying files from one file to another file by davido
in thread Copying files from one file to another file by harishnv

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.