niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » Perl
How to print at certain point in perl
Post new topic   Reply to topic Page 1 of 1 [14 Posts] View previous topic :: View next topic
Author Message
DJ Stunks
*nix forums Guru Wannabe


Joined: 20 Dec 2005
Posts: 192

PostPosted: Thu Jul 20, 2006 1:53 pm    Post subject: Re: How to print at certain point in perl Reply with quote

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

PostPosted: Thu Jul 20, 2006 1:39 pm    Post subject: Re: How to print at certain point in perl Reply with quote

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

PostPosted: Thu Jul 20, 2006 12:48 pm    Post subject: Re: How to print at certain point in perl Reply with quote

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

PostPosted: Thu Jul 20, 2006 12:08 pm    Post subject: Re: How to print at certain point in perl Reply with quote

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

PostPosted: Thu Jul 20, 2006 7:23 am    Post subject: Re: How to print at certain point in perl Reply with quote

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

PostPosted: Thu Jul 20, 2006 4:52 am    Post subject: Re: How to print at certain point in perl Reply with quote

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

PostPosted: Thu Jul 20, 2006 2:15 am    Post subject: Re: How to print at certain point in perl Reply with quote

Jürgen Exner wrote:
Quote:
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)

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

PostPosted: Thu Jul 20, 2006 1:51 am    Post subject: Re: How to print at certain point in perl Reply with quote

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

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

PostPosted: Thu Jul 20, 2006 12:51 am    Post subject: Re: How to print at certain point in perl Reply with quote

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

PostPosted: Wed Jul 19, 2006 5:10 pm    Post subject: Re: How to print at certain point in perl Reply with quote

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

PostPosted: Wed Jul 19, 2006 5:07 pm    Post subject: Re: How to print at certain point in perl Reply with quote

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

PostPosted: Wed Jul 19, 2006 4:58 pm    Post subject: Re: How to print at certain point in perl Reply with quote

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

PostPosted: Wed Jul 19, 2006 4:32 pm    Post subject: Re: How to print at certain point in perl Reply with quote

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

PostPosted: Wed Jul 19, 2006 4:08 pm    Post subject: How to print at certain point in perl Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [14 Posts] View previous topic :: View next topic
The time now is Thu Dec 04, 2008 3:53 am | All times are GMT
navigation Forum index » Programming » Perl
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Unable to print PDF only. subhankar RedHat 0 Mon May 05, 2008 9:08 am
No new posts Need Help with Program in Perl on a Netware Server fhadzocos@gmail.com Perl 3 Fri Jul 21, 2006 1:57 pm
No new posts problems using oddmuse with mod_perl2 inside apache2.2 pe... Fergus McMenemie Perl 0 Fri Jul 21, 2006 9:48 am
No new posts Does stream I/O support "%a" floating-point format? John Friedland C++ 3 Fri Jul 21, 2006 9:26 am
No new posts Problem with Win32-SerialPort over bluetooth @ windows + ... ctloh Perl 0 Fri Jul 21, 2006 8:08 am

Loans | Car Loans | Credit Cards | Adverse Credit Remortgage | Mortgages
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.2347s ][ Queries: 20 (0.1173s) ][ GZIP on - Debug on ]