Dear Most Merciful and Benevolent Monks,

I come with utter supplication and freely admit my utter ignorance and sloth. I plead with you to have extreme mercy and patience so as to accept my most basic request.

I have a text file called funtime.dat that is comma separated values. Here is a small snippet:

IM,BEN01,D,0 IM,BEN02,D,0 IM,BEN03, ,0 IM,BEN04, ,0 IM,BEN05, ,0 IM,BEN06,C,0 IM,BEN07, ,0 IM,BEN08, ,0 IM,BEN09, ,0 IM,BEN10,D,0

My Perl code is thus and works with the following output included below:

#!/usr/bin/perl open(MYINPUTFILE, "<funtime.dat"); while(<MYINPUTFILE>) { my($line) = $_; chomp($line); @data = split(/,/, $line); if ((@data[2] eq "D")||(@data[2] eq "C")) { printf ("\n type "); print @data[2]." "; print @data[1].".txt "; } else { print @data[1].".txt "; } } close(MYINPUTFILE);

My output is

type D BEN01.txt type D BEN02.txt BEN03.txt BEN04.txt BEN05.txt type C BEN06.txt BEN07.txt BEN08.txt BEN09.txt type D BEN10.txt

Actually what I want is it to concatenate the files into the first file name:

type D BEN01.txt > BEN01.txt type D BEN02.txt BEN03.txt BEN04.txt BEN05.txt >BEN02.txt type C BEN06.txt BEN07.txt BEN08.txt BEN09.txt > BEN06.txt type D BEN10.txt > BEN10.txt

Please disregard the C and D as those I use for debugging purposes. Obviously I would then pipe the output of the perl program into a .BAT program which I would execute in MS-DOS.

I believe I should be using some global values instead of local values or something along those lines. In terms of when the value of the line is a "D" or a "C", it gets text file stored as a value until another "D" or "C" value is read and then it prints out a "> textfile.txt" and the makes a newline and prints out "type" and so on.

If this is too difficult how about using the last file before the document has a "D" or "C" to pipe it out to such as this following example:

type D BEN01.txt > BEN01.txt type D BEN02.txt BEN03.txt BEN04.txt BEN05.txt > BEN05.txt type C BEN06.txt BEN07.txt BEN08.txt BEN09.txt >BEN09.txt type D BEN10.txt > BEN10.txt

You might ask why don't I just concatenate the files in Perl. The thing is I am getting the .dat file and therefore it is an extra step. These .dat files can actually be 1,000,000+ lines so that is why I am using Perl to automate this process.

Thank you so much for your assistance.


In reply to Global vs. Local Variables Assistance Requested by Knoperl

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.