Congratulations on a fine choice of programming language ;o)
A few really good habits to start with are:
This takes away the niceness of not having to declare variables, but makes spotting bugs waaaay faster ;)#!/usr/bin/perl -w use strict;
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 ;)$addthis=join("", <TEXT>);
I prefer: open FOO,'foo.txt' or die "$! foo.txt\n";open (FILES,"files.txt") || die "Could not open file: $! \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:
Now $someperl should contain "Some data".#!/usr/bin/perl -w use strict; $someperl=<DATA>; __DATA__ 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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |