| Author |
Message |
DJ Stunks *nix forums Guru Wannabe
Joined: 20 Dec 2005
Posts: 192
|
Posted: Thu Jul 20, 2006 1:53 pm Post subject:
Re: How to print at certain point in perl
|
|
|
Bart Van der Donck wrote:
| Quote: | Just for the hi-score, actually the whole block:
for (split /\n/, $lines) {
my $sp = '';
$sp.=' ' for ( 1 .. ($max + $spaces - length($_)) );
print $_ . $sp . $str . "\n";
}
could be rewritten as:
print $_ .' 'x($max+$spaces-length($_)).$str."\n"for(split/\n/,$lines);
|
I didn't know there was a hi-score involved! My contribution:
#!/usr/bin/perl
use strict;
use warnings;
use Text::Table;
my $tb = Text::Table->new();
$tb->add($_,'abc') while <DATA>;
print $tb;
__DATA__
wwwwwwwwwwwwwwwwwwwwwwwwwwww
qqqqqqqqqqqqqqqqqqqqq
zzzzzzzzzzzzzzzzzzzzzzzzzzzz
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
-jp |
|
| Back to top |
|
 |
Bart Van der Donck *nix forums Guru Wannabe
Joined: 15 May 2005
Posts: 139
|
Posted: Thu Jul 20, 2006 1:39 pm Post subject:
Re: How to print at certain point in perl
|
|
|
Jürgen Exner wrote:
| Quote: | Bart Van der Donck wrote:
my $sp = '';
$sp.=' ' for ( 1 .. ($max + $spaces - length($_)) );
Did you mean
my $sp = ' ' x ($max + $spaces - length($_));
|
Just for the hi-score, actually the whole block:
for (split /\n/, $lines) {
my $sp = '';
$sp.=' ' for ( 1 .. ($max + $spaces - length($_)) );
print $_ . $sp . $str . "\n";
}
could be rewritten as:
print $_ .' 'x($max+$spaces-length($_)).$str."\n"for(split/\n/,$lines);
--
Bart |
|
| Back to top |
|
 |
Jürgen Exner *nix forums Guru
Joined: 09 Apr 2005
Posts: 514
|
Posted: Thu Jul 20, 2006 12:48 pm Post subject:
Re: How to print at certain point in perl
|
|
|
Bart Van der Donck wrote:
| Quote: | my $sp = '';
$sp.=' ' for ( 1 .. ($max + $spaces - length($_)) );
|
Did you mean
my $sp = ' ' x ($max + $spaces - length($_));
jue |
|
| Back to top |
|
 |
Tad McClellan *nix forums Guru
Joined: 09 Mar 2005
Posts: 1647
|
Posted: Thu Jul 20, 2006 12:08 pm Post subject:
Re: How to print at certain point in perl
|
|
|
Bart Van der Donck <bart@nijlen.com> wrote:
| Quote: | Amaninder wrote:
I want to print something, lets say "abc", at certain point on the
screen no matter whats come before it.
|
| Quote: | for (split /\n/, $lines) {
my $sp = '';
$sp.=' ' for ( 1 .. ($max + $spaces - length($_)) );
print $_ . $sp . $str . "\n";
}
|
Or let printf do it for you by replacing that loop body with:
printf "%-${max}s%${spaces}s%s\n", $_, ' ', $str;
or the same thing formatted for human consumption:
printf "%-${max}s" # the variable length string
. "%${spaces}s" # the offset between columns
. "%s\n" # the string to be aligned
, $_
, ' '
, $str;
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas |
|
| Back to top |
|
 |
Bart Van der Donck *nix forums Guru Wannabe
Joined: 15 May 2005
Posts: 139
|
Posted: Thu Jul 20, 2006 7:23 am Post subject:
Re: How to print at certain point in perl
|
|
|
Amaninder wrote:
| Quote: | I want to print something, lets say "abc", at certain point on the
screen no matter whats come before it. For example, if there are
following lines
wwwwwwwwwwwwwwwwwwwwwwwwwwww
qqqqqqqqqqqqqqqqqqqqqq
zzzzzzzzzzzzzzzzzzzzzzzzzzzz
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
i want to print "abc" always at fixed length from the left. Like
wwwwwwwwwwwwwwwwwwwwwwwwwwww abc
qqqqqqqqqqqqqqqqqqqqq abc
zzzzzzzzzzzzzzzzzzzzzzzzzzzz abc
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee abc
|
#!/usr/bin/perl
use strict;
use warnings;
my $max = 0;
# what are the lines ?
my $lines = 'wwwwwwwwwwwwwwwwwwwwwwwwwwww
qqqqqqqqqqqqqqqqqqqqq
zzzzzzzzzzzzzzzzzzzzzzzzzzzz
eeeeeeeeeeeeeeeeeeeeee';
# what is the string to display next to each line ?
my $str = 'abc';
# how many spaces between longest line and $str ?
my $spaces = 2;
for (split /\n/, $lines) {
$max = length($_) if length($_) > $max;
}
for (split /\n/, $lines) {
my $sp = '';
$sp.=' ' for ( 1 .. ($max + $spaces - length($_)) );
print $_ . $sp . $str . "\n";
}
__END__
Hope this helps,
--
Bart |
|
| Back to top |
|
 |
Jürgen Exner *nix forums Guru
Joined: 09 Apr 2005
Posts: 514
|
Posted: Thu Jul 20, 2006 4:52 am Post subject:
Re: How to print at certain point in perl
|
|
|
DJ Stunks wrote:
| Quote: | Jürgen Exner wrote:
Amaninder wrote:
I want to print something, lets say "abc", at certain point on the
screen no matter whats come before it.
The Curses module is your friend, see
http://search.cpan.org/author/GIRAFFED/Curses-1.14/gen/make.Curses.pm
For example, if there are
following lines
(repaired by J. Peavy)
wwwwwwwwwwwwwwwwwwwwwwwwwwww
qqqqqqqqqqqqqqqqqqqqqq
zzzzzzzzzzzzzzzzzzzzzzzzzzzz
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
i want to print "abc" always at fixed length from the left. Like
wwwwwwwwwwwwwwwwwwwwwwwwwwww abc
qqqqqqqqqqqqqqqqqqqqq abc
zzzzzzzzzzzzzzzzzzzzzzzzzzzz abc
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee abc
so how would you perform this task with Curses? I've never used it,
|
Well, Curses allows you to position the cursor at any point on the screen,
just like the OP asked:
<quote>
I want to print something [...] at certain point on the screen no matter
whats come before it.
</quote>
| Quote: | but if I was solving this problem I would use Text::Table so I didn't
have to determine the length of the longest line ahead of time...
|
I was assuming that he already knew _where_ he wants to print the abc. In
particular because he wants to print at that position "no matter what come
before it" I would guess that the preceeding text in that line has no effect
on the desired position of abc.
Of course his description (I refuse to call that a specification) is so
vague, that any interpretation may be correct.
jue |
|
| Back to top |
|
 |
DJ Stunks *nix forums Guru Wannabe
Joined: 20 Dec 2005
Posts: 192
|
Posted: Thu Jul 20, 2006 2:15 am Post subject:
Re: How to print at certain point in perl
|
|
|
Jürgen Exner wrote:
(repaired by J. Peavy)
| Quote: | wwwwwwwwwwwwwwwwwwwwwwwwwwww
qqqqqqqqqqqqqqqqqqqqqq
zzzzzzzzzzzzzzzzzzzzzzzzzzzz
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
i want to print "abc" always at fixed length from the left. Like
wwwwwwwwwwwwwwwwwwwwwwwwwwww abc
qqqqqqqqqqqqqqqqqqqqq abc
zzzzzzzzzzzzzzzzzzzzzzzzzzzz abc
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee abc
|
so how would you perform this task with Curses? I've never used it, but
if I was solving this problem I would use Text::Table so I didn't have
to determine the length of the longest line ahead of time...
-jp |
|
| Back to top |
|
 |
Dr.Ruud *nix forums Guru
Joined: 23 Sep 2005
Posts: 721
|
Posted: Thu Jul 20, 2006 1:51 am Post subject:
Re: How to print at certain point in perl
|
|
|
Amaninder schreef:
| Quote: | I dont know how to post in
proportional font
|
I assumed that you composed your posting while having some proportional
font active, which is not a good idea on technical newsgroups in
general, but especially not when you address outlining of text.
| Quote: | but if you click the "Proportional Font" at the top
right corner, you can see what i mean.
|
There are hundreds of proportional fonts available on this system, each
with its own characteristics concerning character widths and spacing
etc., so I strongly doubt that I will see what you seem to think that
you mean.
--
Affijn, Ruud
"Gewoon is een tijger." |
|
| Back to top |
|
 |
Jürgen Exner *nix forums Guru
Joined: 09 Apr 2005
Posts: 514
|
Posted: Thu Jul 20, 2006 12:51 am Post subject:
Re: How to print at certain point in perl
|
|
|
Amaninder wrote:
| Quote: | I want to print something, lets say "abc", at certain point on the
screen no matter whats come before it.
|
The Curses module is your friend, see
http://search.cpan.org/author/GIRAFFED/Curses-1.14/gen/make.Curses.pm
| Quote: | For example, if there are
following lines
wwwwwwwwwwwwwwwwwwwwwwwwwwww
qqqqqqqqqqqqqqqqqqqqqq
zzzzzzzzzzzzzzzzzzzzzzzzzzzz
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
i want to print "abc" always at fixed length from the left. Like
wwwwwwwwwwwwwwwwwwwwwwwwwwww abc
qqqqqqqqqqqqqqqqqqqqq abc
zzzzzzzzzzzzzzzzzzzzzzzzzzzz abc
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee abc
|
What are you talking about? None of those lines has anything fixed to it.
They are all of different length and the abc is offset by different numbers
of characters from the preceeding text and the abc's are not aligned by any
means.
jue |
|
| Back to top |
|
 |
David Squire *nix forums Guru Wannabe
Joined: 08 Apr 2006
Posts: 197
|
Posted: Wed Jul 19, 2006 5:10 pm Post subject:
Re: How to print at certain point in perl
|
|
|
Amaninder wrote:
| Quote: | Yes, indeed its a non-proportinal font.
|
What? To whom are you replying? Please quote some context and retain
author attribution when replying, as has been the custom on usenet for
decades.
| Quote: | I dont know how to post in
proportional font but if you click the "Proportional Font" at the top
right corner, you can see what i mean. :)
|
Again, what are you talking about? Let me guess... the Google Groups
interface? You seem to be under the mistaken impression that you are
posting to some forum run by Google. You are not. You are posting to a
usenet newsgroup and many, probably most, of the readers are using
dedicated newsreader software, or newsgroup-aware mailers such as
Thunderbird.
There is no font associated with a newsgroup post. It's just characters.
The issue is that *you* should compose your post using a
non-proportional font if you want spacing to be significant on usenet.
DS |
|
| Back to top |
|
 |
Glenn Jackman *nix forums addict
Joined: 19 Apr 2005
Posts: 97
|
Posted: Wed Jul 19, 2006 5:07 pm Post subject:
Re: How to print at certain point in perl
|
|
|
At 2006-07-19 12:08PM, Amaninder <Amaninder.Saini@gmail.com> wrote:
| Quote: | i want to print "abc" always at fixed length from the left. Like
|
try "printf"
--
Glenn Jackman
Ulterior Designer |
|
| Back to top |
|
 |
Amaninder.Saini@gmail.com *nix forums beginner
Joined: 02 Jun 2006
Posts: 11
|
Posted: Wed Jul 19, 2006 4:58 pm Post subject:
Re: How to print at certain point in perl
|
|
|
Yes, indeed its a non-proportinal font. I dont know how to post in
proportional font but if you click the "Proportional Font" at the top
right corner, you can see what i mean. :)
Regards
Amaninder |
|
| Back to top |
|
 |
Dr.Ruud *nix forums Guru
Joined: 23 Sep 2005
Posts: 721
|
Posted: Wed Jul 19, 2006 4:32 pm Post subject:
Re: How to print at certain point in perl
|
|
|
Amaninder schreef:
| Quote: | wwwwwwwwwwwwwwwwwwwwwwwwwwww abc
qqqqqqqqqqqqqqqqqqqqq abc
zzzzzzzzzzzzzzzzzzzzzzzzzzzz abc
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee abc
|
I think you are posting in a non-proportional font, since the length of
thye strings before 'abc' are all of different lengths.
perl -wple '
$n = 40 ;
$_ = substr( $_ . q{ } x $n, 0, $n ) . q{abc} ;
' infile
--
Affijn, Ruud
"Gewoon is een tijger." |
|
| Back to top |
|
 |
Amaninder.Saini@gmail.com *nix forums beginner
Joined: 02 Jun 2006
Posts: 11
|
Posted: Wed Jul 19, 2006 4:08 pm Post subject:
How to print at certain point in perl
|
|
|
Hi everyone
I am new to perl so please help
I want to print something, lets say "abc", at certain point on the
screen no matter whats come before it. For example, if there are
following lines
wwwwwwwwwwwwwwwwwwwwwwwwwwww
qqqqqqqqqqqqqqqqqqqqqq
zzzzzzzzzzzzzzzzzzzzzzzzzzzz
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
i want to print "abc" always at fixed length from the left. Like
wwwwwwwwwwwwwwwwwwwwwwwwwwww abc
qqqqqqqqqqqqqqqqqqqqq abc
zzzzzzzzzzzzzzzzzzzzzzzzzzzz abc
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee abc
I dont know how to do it in perl. If someone did this before then
please let me know. :)
Regards
Amaninder |
|
| Back to top |
|
 |
Google
|
|
| Back to top |
|
 |
|