I am writing my first perl program at the moment (see below). What I want to do is insert the contents of general.txt into the start of all of the files listed in a file called files.txt. The code below works for the first file listed in files.txt but I can't figure out how to get it to loop through all of the files in files.txt. I thought of counting the number of lines in files.txt and using a 'for' loop, but that's a bit inelegant. Can you help? S.
#!/usr/local/bin/perl open (TEXT,"general.txt") || die "Could not open file: $! \n"; $addthis=join("", <TEXT>); close (TEXT); open (FILES,"files.txt") || die "Could not open file: $! \n"; # Start to loop here??? or after the next group of statements? $line=<FILES>; chomp $line; open (DATA,"$line") || die "Could not open file: $! \n"; $tothis=join("", <DATA>); close (DATA); $newrecord=$addthis.$tothis; open (DATA,">$line") || die "Could not write to: $! \n"; print "$line\n"; print DATA $newrecord; close (DATA);

In reply to How to Put a Loop in my 1st Program by shemyaza

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.