Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

printing sentences on the same line

426 views
Skip to first unread message

Ryan

unread,
Jan 6, 2008, 7:48:11 PM1/6/08
to begi...@perl.org
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?

Thanks.

--Chris Ryan

Chas. Owens

unread,
Jan 6, 2008, 7:52:25 PM1/6/08
to Ryan, begi...@perl.org

Use chomp() to remove the "\n" on the end of each sentence. Remember
to output at "\n" at the end.

Yitzle

unread,
Jan 6, 2008, 9:51:03 PM1/6/08
to Chas. Owens, Ryan, begi...@perl.org
In addition to what Chas said, you may also want to add a space after
each sentence.

Gunnar Hjalmarsson

unread,
Jan 7, 2008, 3:20:20 AM1/7/08
to begi...@perl.org
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.
>
> 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

Ryan

unread,
Jan 12, 2008, 6:44:27 PM1/12/08
to begi...@perl.org
This approach did the job. Thanks.

--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.

>

John W. Krahn

unread,
Jan 12, 2008, 7:29:37 PM1/12/08
to begi...@perl.org
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;
}

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

Gunnar Hjalmarsson

unread,
Jan 12, 2008, 7:50:21 PM1/12/08
to begi...@perl.org
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. ;-)

John W. Krahn

unread,
Jan 12, 2008, 8:07:48 PM1/12/08
to Perl Beginners
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/;

Gunnar Hjalmarsson

unread,
Jan 12, 2008, 9:00:46 PM1/12/08
to begi...@perl.org
> 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

John W. Krahn

unread,
Jan 12, 2008, 10:19:19 PM1/12/08
to Perl Beginners

OK, I'll concede that I may have misread or not understood the OP's
objectives correctly. :-)

0 new messages