How to print at certain point in perl

Discussion in 'Perl Misc' started by Amaninder, Jul 19, 2006.

  1. Amaninder

    Amaninder Guest

    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
    Amaninder, Jul 19, 2006
    #1
    1. Advertising

  2. Amaninder

    Dr.Ruud Guest

    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

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Jul 19, 2006
    #2
    1. Advertising

  3. Amaninder

    Amaninder Guest

    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
    Amaninder, Jul 19, 2006
    #3
  4. Amaninder

    David Squire Guest

    Amaninder wrote:
    > 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
    David Squire, Jul 19, 2006
    #4
  5. 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
    >
    > 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
    Jürgen Exner, Jul 20, 2006
    #5
  6. Amaninder

    Dr.Ruud Guest

    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.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Jul 20, 2006
    #6
  7. Amaninder

    DJ Stunks Guest

    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, 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
    DJ Stunks, Jul 20, 2006
    #7
  8. DJ Stunks wrote:
    > 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>

    > 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
    Jürgen Exner, Jul 20, 2006
    #8
  9. Amaninder wrote:

    > 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
    Bart Van der Donck, Jul 20, 2006
    #9
  10. Bart Van der Donck <> wrote:
    > Amaninder wrote:
    >
    >> I want to print something, lets say "abc", at certain point on the
    >> screen no matter whats come before it.



    > 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
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jul 20, 2006
    #10
  11. Bart Van der Donck wrote:
    > my $sp = '';
    > $sp.=' ' for ( 1 .. ($max + $spaces - length($_)) );


    Did you mean
    my $sp = ' ' x ($max + $spaces - length($_));

    jue
    Jürgen Exner, Jul 20, 2006
    #11
  12. Jürgen Exner wrote:

    > 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
    Bart Van der Donck, Jul 20, 2006
    #12
  13. Amaninder

    DJ Stunks Guest

    Bart Van der Donck wrote:
    > 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
    DJ Stunks, Jul 20, 2006
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    637
  2. keto
    Replies:
    0
    Views:
    898
  3. Replies:
    2
    Views:
    505
    bruce barker
    Mar 25, 2008
  4. Saraswati lakki
    Replies:
    0
    Views:
    1,287
    Saraswati lakki
    Jan 6, 2012
  5. SAN CAZIANO
    Replies:
    8
    Views:
    165
    Dr John Stockton
    Oct 15, 2004
Loading...

Share This Page