Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Multiple Rows

by mtmcc (Hermit)
on Oct 04, 2013 at 07:10 UTC ( [id://1056843]=note: print w/replies, xml ) Need Help??


in reply to Multiple Rows

First, it's worth using strict and warnings.

Second, when you configure the text of the label in a loop, you literally do just that, ie you set the text of the label to the current value you assign to it.

If you want a multiline label, I'd join the individual lines together into a single string before printing it, like this:

#!/usr/bin/perl use Tk; use strict; use warnings; my $window = MainWindow->new; $window -> geometry("100x200"); $window->title("Host Report"); my $labs = $window->Label(-text => "Results", -wraplength => 100)->pac +k(-side => 'bottom'); #$window->Entry(-textvariable => \$hdb )->pack; $window->Button(-text => "Go", -command => \&host )->pack; $window->Button(-text => "Quit", -command => \&stop )->pack; MainLoop; sub host { my @message; #open (FH,"host.txt"); my @lines = <DATA>; #close FH; foreach my $hdb2 (@lines) { my @field = split(':',$hdb2); #if ($field[0] =~ /(?<![\w-])$hdb(?![\w-])/i) { push (@message, "$field[0] $field[1] $field[2]"); #} } my $messageString = join ('', @message); $labs->configure(-text =>"$messageString"); } sub stop{ exit; } __DATA__ these: lines are: the: lines that: you are: looking: at

Alternatively, you could use a widget other than a label, maybe something like this:

!/usr/bin/perl use strict; use warnings; use Tk; use Tk::ROText; my $hdb; my $window = MainWindow->new; $window->title("Host Report"); my $labs = $window->Scrolled('ROText')->pack(-side => 'bottom'); $labs -> Insert ("Results\n\n"); $window->Entry(-textvariable => \$hdb )->pack; $window->Button(-text => "Go", -command => \&host )->pack; $window->Button(-text => "Quit", -command => \&stop )->pack; MainLoop; sub host { #open (FH, "<", "host.txt"); my @lines = <DATA>; print STDERR "@lines\n\n"; #close FH; foreach my $hdb2 (@lines) { my @field = split(':',$hdb2); #if ($field[0] =~ /(?<![\w-])$hdb(?![\w-])/i) { $labs->Insert("$field[0] $field[1] $field[2]"); #} } } sub stop{ exit; } __DATA__ these: lines are: the: lines that: you are: looking: at

I hope that's helpful

Replies are listed 'Best First'.
Re^2: Multiple Rows
by PilotinControl (Pilgrim) on Oct 04, 2013 at 13:39 UTC

    Both Options worked perfectly thanks!

Re^2: Multiple Rows
by PilotinControl (Pilgrim) on Oct 04, 2013 at 18:15 UTC

    What format method would you suggest mtmcc to line up the rows? Thanks.

      I'm glad you've made some progress.

      I'm not certain what you mean by lining up the rows; if you mean aligning the columns, then you could do something like use a table widget, instead of a label/ROText, like this:

      #!/usr/bin/perl use strict; use warnings; use Tk; use Tk::ROText; use Tk::Table; my $hdb; my $window = MainWindow->new; $window->title("Host Report"); my $labs = $window->Table(-columns => 3, -rows => 5)->pack(-side => 'b +ottom'); $labs -> put (1, 1, "Results"); $window->Entry(-textvariable => \$hdb )->pack; $window->Button(-text => "Go", -command => \&host )->pack; $window->Button(-text => "Quit", -command => \&stop )->pack; MainLoop; sub host { #open (FH, "<", "host.txt"); my $row = 2; my $col = 0; my $x = 0; my @lines = <DATA>; print STDERR "@lines\n\n"; #close FH; foreach my $hdb2 (@lines) { chomp $hdb2; my @field = split(':',$hdb2); #if ($field[0] =~ /(?<![\w-])$hdb(?![\w-])/i) { for ($x=0; $x <=2; $x += 1) { $field[$x] = '' unless defined $field[$x]; my $tempLabel = $labs->Label(-text =>"$field[$x]", -anchor => +'w'); $labs->put($row, $x, $tempLabel); } $row += 1; #} } } sub stop{ exit; } __DATA__ these:lines are:the:lines that:you are:looking:at

      Alternatively, you could get all the data you want to print in an array or other data structure, and use a text based approach to alignment (eg see 'padding with spaces' here: Using (s)printf())

      If you meant something else, let me know and I'll try again...

        do not really need the scroll text bars...all is needed is to search the host file and have a header with the results posted below:
        IP ADDRESS | PORT | TCP
        192.168.1.1

        Thats more like the out put I am seeking thanks.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-03-19 02:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found