For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > January 2008 > printing sentences on the same line









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author printing sentences on the same line
Ryan

2008-01-06, 7:01 pm

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

2008-01-06, 7:01 pm

On Jan 6, 2008 7:48 PM, Ryan <cryan2@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.
>
> 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.
Yitzle

2008-01-06, 10:05 pm

In addition to what Chas said, you may also want to add a space after
each sentence.
Gunnar Hjalmarsson

2008-01-07, 4:00 am

Chas. Owens wrote:
> On Jan 6, 2008 7:48 PM, Ryan <cryan2@stny.rr.com> wrote:
>
> 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

2008-01-12, 7:02 pm

This approach did the job. Thanks.

--Chris Ryan

Gunnar Hjalmarsson wrote:
> Chas. Owens wrote:
[color=darkred]
>
> 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";
>

John W. Krahn

2008-01-12, 7:02 pm

Ryan wrote:
>
> Gunnar Hjalmarsson wrote:
>
> 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

2008-01-12, 7:02 pm

John W. Krahn wrote:
> Ryan 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. ;-)

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
John W. Krahn

2008-01-12, 10:05 pm

Gunnar Hjalmarsson wrote:
> John W. Krahn wrote:
>
> 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
Gunnar Hjalmarsson

2008-01-12, 10:05 pm

John W. Krahn wrote:
> Gunnar Hjalmarsson wrote:
>
> From the OP's original post:
>
>
> 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
John W. Krahn

2008-01-12, 10:05 pm

Gunnar Hjalmarsson wrote:
> John W. Krahn wrote:
>
> 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. :-)


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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com