Gunnar Hjalmarsson wrote:
John W. Krahn wrote:
Ryan wrote:
Gunnar Hjalmarsson wrote:
One way to do that is to store the sentences in an output array instead of printing them directly.

Within the while loop:

    push @output, ...;

And then:

    chomp @output;
    print "@output\n";

This approach did the job.  Thanks.

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.

Any suggestions how to accomplish this?

Could you please explain how the code I posted does not accomplish the OP's objectives or is inefficient?

Perhaps the phrase "contingent upon what is found in each line of data." is what you are objecting to? If so, change:

    print;

To:

    print if /some condition/;



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to