How to print at certain point in perl

A

Amaninder

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
 
D

Dr.Ruud

Amaninder schreef:
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
 
A

Amaninder

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
 
D

David Squire

Amaninder said:
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.
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
 
J

Jürgen Exner

Amaninder said:
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

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
 
D

Dr.Ruud

Amaninder schreef:
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.

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

Jürgen Exner

DJ said:
(repaired by J. Peavy)


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

Bart Van der Donck

Amaninder said:
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,
 
T

Tad McClellan

Bart Van der Donck said:
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;
 
B

Bart Van der Donck

Jürgen Exner said:
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);
 
D

DJ Stunks

Bart said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top