John W. Krahn wrote:
Gunnar Hjalmarsson wrote:
John W. Krahn wrote:
A more efficient method is to read and write one line at a time:
$\ = ' '; # set Output Record Separator to a space
while ( <> ) {
$\ = "\n" if eof;
chomp;
print;
}
That may be an efficient solution to some other problem but the one
the OP initially presented. ;-)
From the OP's original post:
I have a small piece of a program which loops through lines of data,
using the <while> construct, one line at a time, and prints different
pre-defined sentences, contingent upon what is found in each line of
data. The data are in the program file, in a __DATA__ section. I use
the simple print command.
The output looks like this:
This is the first sentence.
This is the next sentence.
And this is the third one.
etc.
I'd like the output to look like this:
This is the first sentence. This is the next sentence. And this is the
third one. etc.
Could you please explain how the code I posted does not accomplish the
OP's objectives or is inefficient?
Well, to me it seems like the pre-defined sentences, including the
undesired newline symbols, are not part of the data that is read by the
while loop. This code illustrates my interpretation of the OP's problem:
my %sentences = (
first => "This is the first sentence.\n",
second => "This is the second sentence.\n",
third => "This is the third sentence.\n",
fourth => "This is the fourth sentence.\n",
);
my @output;
while (<DATA>) {
chomp;
push @output, $sentences{$_} if $sentences{$_};
}
chomp @output;
print "@output\n";
__DATA__
first
second
fourth
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/