in reply to How to Put a Loop in my 1st Program
I tried to follow your methodology as closely as possible. There are easier and/or more efficient ways of doing what you're trying to do, but personally, I'd worry about style after you're a little more comfortable with the language as a whole. Not to discount style, mind you, but as with a spoken language, you don't worry about writing a sonnet or a haiku until you've learned how to write a sentence or paragraph.
Update: Pursuant to discussion with tilly on the use of our vs. use vars, I've modified the above code to use vars rather than our as I had done originally. I wasn't aware that our differed so much in theory and practice.
Update2: Eeeeeek! Hofmator is absolutely correct. I had undef $/; in the wrong place. Thanks for catching that, Hofmator. I moved it down a few lines, and it will work correctly now. I'm now performing undef $/; after we've read in "files.txt".
___________________
Kurt
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 ); }
Update: Pursuant to discussion with tilly on the use of our vs. use vars, I've modified the above code to use vars rather than our as I had done originally. I wasn't aware that our differed so much in theory and practice.
Update2: Eeeeeek! Hofmator is absolutely correct. I had undef $/; in the wrong place. Thanks for catching that, Hofmator. I moved it down a few lines, and it will work correctly now. I'm now performing undef $/; after we've read in "files.txt".
___________________
Kurt
|
---|
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 |
In Section
Seekers of Perl Wisdom