Congratulations on a fine choice of programming language ;o)

A few really good habits to start with are:

#!/usr/bin/perl -w use strict;
This takes away the niceness of not having to declare variables, but makes spotting bugs waaaay faster ;)

$addthis=join("", <TEXT>);
Whoa!! Nice use of join, it makes sense now I think about it. I'd suggest using ' instead of " as " interpolates stuff, and if you don't need it use ' instead ;)
open (FILES,"files.txt") || die "Could not open file: $! \n";
I prefer: open FOO,'foo.txt' or die "$! foo.txt\n";


Maybe use a while loop or a for loop, as for loops can traverse arrays too.

for (<FILES>){ chomp; # each item of the array is placed in $_ for the scope of t +he for unless (open (D,$_)){ # no need to wrap the variable in quotes print STDERR "$!:$_\n"; # In case the failure to read one file + isn't critical next; # Skips all the other commands in the loop and goes to t +he next iteration. } my $newrecord=$addthis.join('',<D>); close(D); open (D,">$_") || die "Could not write to: $! \n"; print "$_\n"; print D $newrecord; close (D) }

Watch yourself here, DATA is a reserved word IIRC like:

#!/usr/bin/perl -w use strict; $someperl=<DATA>; __DATA__ Some data
Now $someperl should contain "Some data".

--

Brother Frankus.

¤


In reply to Re: How to Put a Loop in my 1st Program by frankus
in thread 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.