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?
Thanks.
--Chris Ryan
Use chomp() to remove the "\n" on the end of each sentence. Remember
to output at "\n" at the end.
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";
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
--Chris Ryan
Gunnar Hjalmarsson wrote:
> Chas. Owens wrote:
>> On Jan 6, 2008 7:48 PM, Ryan <cry...@stny.rr.com> wrote:
>>> 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.
>
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;
}
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
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/;
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
OK, I'll concede that I may have misread or not understood the OP's
objectives correctly. :-)