in reply to How to Put a Loop in my 1st Program
Since you're treating all of the files as individual strings, you can do the undef $/; business. $/ is the input record separator, and since we aren't limiting the scope (i.e., putting undef $/; inside a subroutine, for example), it is a global change and will apply to all subsequently read files. And in this case, I think a 'while' loop is one of the easier ways to setup the looping. I don't want this to sound like a shameless plug, but I wrote the module File::Butler, and it was specifically designed to handle this sort of thing. And since File::Butler does all of the grunt work for you, you don't need to worry about most of it. Using File::Butler, you could re-write the above as this:#!/usr/local/bin/perl use strict; use vars qw( $addthis $tothis $newrecord ); open (TEXT, "general.txt") || die "Couldn't open file: $|\n"; $addthis = <TEXT>; close (TEXT); undef $/; open (FILES,"files.txt") || die "Could not open file: $! \n"; while ( <FILES> ) { open ( DATA, $_ ) || die "Could not open file: $|\n"; $tothis = <DATA>; close (DATA); $newrecord = $addthis . $tothis; open ( DATA, "> $_" ) || die "Could not write to file: $|\n"; print $_, "\n"; print DATA $newrecord; close (DATA); } close (FILES);
I hope this helps. Welcome to the Perl Fraternity, btw. We'll teach you the secret handshake later. :-)#!/usr/local/bin/perl use strict; use File::Butler; use vars qw( @newstuff @files ); Butler( "general.txt", "read", \@newstuff ); Butler( "files.txt", "read", \@files ); foreach my $file ( @files ) { Butler( $file, "prepend", \@newstuff ); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: How to Put a Loop in my 1st Program
by Hofmator (Curate) on Nov 23, 2001 at 00:44 UTC |