Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Re: How to replace Tab with spaces not altering postion

by juo (Curate)
on Oct 09, 2002 at 15:17 UTC ( #203955=note: print w/ replies, xml ) Need Help??


in reply to Re: How to replace Tab with spaces not altering postion
in thread How to replace Tab with spaces not altering postion

Text::Tabs will do the same as my example, it will replace the tabs with a fixed amount of spaces but it will not consider the spaces in between the two strings. If you make a tab it can contain one time 2 spaces, another time it contains 5 spaces, .... so if you replace the tabs with a fixed amount of spaces the positioning will be scrambled. No collumn effect anymore. So to make it work you first would have to check the amount of spaces between the two strings and then replace the tab with the amount of spaces it was representing. Sorry the Text::Tabs is working fine. It was a proportional font that was all the time giving the bad result and giving the impression that the spaces were incorrect. It is sometimes in a detail.


Comment on Re: Re: How to replace Tab with spaces not altering postion
Re^3: How to replace Tab with spaces not altering postion
by particle (Vicar) on Oct 09, 2002 at 15:39 UTC

    Text::Tabs will do the same as my example, it will replace the tabs with a fixed amount of spaces but it will not consider the spaces in between the two strings.

    i disagree. from the description in the Text::Tabs pod:

    Text::Tabs does about what the unix utilities expand(1) and unexpand(1) do. Given a line with tabs in it, expand will replace the tabs with the appropriate number of spaces. Given a line with or without tabs in it, unexpand will add tabs when it can save bytes by doing so. Invisible compression with plain ascii!

    here's an example:

    #!/usr/bin/perl require 5.006_001; use strict; use warnings; $|++; use Text::Tabs; my $tabstop = 8; my $text = "12345678901234567890\n\thi\tbye"; print $text,$/,length $text,$/; my $newtext = expand($text) print $newtext, $/,length $newtext,$/; __END__ ## prints 12345678901234567890 hi bye 28 12345678901234567890 hi bye 40

    ...unless i'm missing something

    ~Particle *accelerates*

      You are right, it should work. However with me it is not working and I guess this is a bug or something else in the listbox module of Tk. Below you can find an example using your example were it comes out bad in the listbox.

      #!/usr/bin/perl -w use strict; use warnings; use Tk; $|++; my $W; my $c; my $lab; my @newtext; use Text::Tabs; my $tabstop = 8; my $text = "Shape:r59\tX-loc:9.77\tY-loc:5.608"; my $text1 = "Shape:r59\tX-loc:9.8819\tY-loc:0"; push @newtext,($text,$text1); my @newtext = expand(@newtext); $c = initCanvas4(); sub initCanvas4 { $W = MainWindow->new; $W->title("Fiducial Information"); $W->iconname('Fiducial Information'); my $canv = $W->Canvas(qw/-relief sunken -relief flat -bd 0 -width 512 +-height 350 -background SlateGray3/); $canv->pack; return $canv; } # Create a listbox with the contents of the directory. + my $ent = $c->Scrolled(qw/Listbox -setgrid 1 -height 3 -width 40 -scro +llbars e -selectmode single/); for(@{newtext}) { $ent->insert(0, "$_"); } #$ent->bind('<Double-Button-1>', # sub { # $file_selected = $ent->get('active'); # # } $c->createWindow('10p 80p', -window => $lab, -anchor => 'nw' ); $c->createWindow('10p 95p', -window => $ent, -anchor => 'nw' ); MainLoop;

        The reason your text doesn't line up is probably because you have a proportional font being used in your list box.

        To display tab-columnated data you will need to instruct the listbox to use a non-proportional font.



        Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!

        Actually, there is a second problem with your sample code, Text::Tabs uses a global var $tabstop for control (Yuck!) but you are using a my var. You will need to change that.



        Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://203955]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (12)
As of 2014-09-30 14:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (373 votes), past polls