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

i have a file f1 which has data in the format.{MCL}i was present{MCL}.i have another file f2 which has values in column format.I need to update f2 where row1 is i<clause_start="MCL"> and last row is present<clause_end="MCL">.for this the code i wrote which is incomplete is

foreach(f1) { chomp; $line=split(/\s*\{\d+\}/,$_); foreach(f2) $_="CLAUSE_START=$line"; if($_ eq EOF) { $_="CLAUSE_END=$line"; } }

Replies are listed 'Best First'.
Re: merging a file with a value present in another file
by frozenwithjoy (Priest) on Jul 13, 2012 at 08:59 UTC
    I'm having difficulty understanding exactly what you want. Perhaps you could include a brief sample of f1, f2 and the desired output.

    A few things you need to cleanup while you are working on this:

    • $line=split ... will only catch the # of elements resulting from the split. Be sure to have an array on the left-hand side, not a scalar
    • Your second foreach is missing it's curly braces
    • I'm not sure what you are trying with $_="CLAUSE_START=$line";, but I don't like seeing $_ on the left-hand side of an assignment
    • This is probably just your pseudocode, but if not, when looping through files, be sure to open and close them within the loop and essentially loop through the elements in your array of filenames (i.e., foreach (@f1_filenames) )

      my file f1 is a tagged data some what in this format.{MCL}why is this{/MCL}.second file is in a column format where the rows are why,is and this. i want to add a field to the column like near the word why field should be<clause_start="mcl"> and near the word this field should be<clause_end="mcl">.

        Please try again: as frozenwithjoy said, give us a sample of file1, a sample of file2, and a sample of what you want the output to be. Wrap each of these three samples in <code></code> tags to preserve their formatting. By a "sample," I mean a few lines that are enough to demonstrate your problem. One line is rarely enough; more than a dozen is usually too many.

        Aaron B.
        Available for small or large Perl jobs; see my home node.