CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Perl Tk: Parsing flatfile and inputing into a text widget

 



uskidsknow
New User

Oct 2, 2011, 6:25 AM

Post #1 of 2 (1557 views)
Perl Tk: Parsing flatfile and inputing into a text widget Can't Post

Hi Guys.

New to Perl and this forum.

I am trying to build a script that acts as a directory for my companies infrastructure. I want to create a tk app that has a search feature and a text widget to display all the details.

I have not got very far but all i want to do at this point is see if I can get any content into the text widget.

the flat file is delimited and in the following format.

SWITCH1;1.1.1.1;IDCC;ACP;CISCO;4506;SSH;TACACS.

i am trying to see from if statements if i can get anything into the text widget. but it is not working .... HELP :)

please see below


#!/usr/bin/perl

use Tk;
use FileHandle;
$mw = MainWindow->new();
$mw->configure(-title => "Directory");
my $frm1 = $mw->Frame();
my $frm2 = $mw->Frame();
$file_name = flatfile.txt;
@switch = ("SWITCH1");

my $search_label = $frm1 -> Label(-text=>"Search For Device. E.g. switch1") -> pack(-side => 'top');
my $search_button = $frm1 -> Button(-text => "Search", -font => "Helvetica -16 bold") -> pack(-side => 'top');

my $text = $frm2->Text() -> pack(-side => 'bottom', -expand => 1, -fill => 'x');

$text->configure(-height => 15,
-background => 'white',
-foreground => 'black',
-wrap => 'word',
-font => "Helvetica -12");


open(FILE, flatfile.txt);
foreach my $line (<FILE>) {
@fields = split /\;/, $_, 8;

if($fields[0] =~ /$switch/) {
WriteRecord(\@fields);
}
}
$text->pack();


my $exit_button = $mw -> Button(-text => "All Done", -font => "Helvetica -12 bold",
-command => sub { exit })
-> pack(-side => 'bottom', -expand => 1, -fill => 'x');

$frm1->pack(-side => 'top');
$frm2->pack(-side => 'top');

MainLoop;
sub WriteRecord
{
$text->insert("1.0", "here is my text to insert");
}


wickedxter
User

Oct 2, 2011, 7:59 PM

Post #2 of 2 (1543 views)
Re: [uskidsknow] Perl Tk: Parsing flatfile and inputing into a text widget [In reply to] Can't Post

one way i can think of is to tie the text area to a variable and update the variable as it will print out the contents of the variable in the text area.. theirs a book im sure its online " Perl TK " which is great and explains a lot in detail.

i wrote this a while back

Code
 

#Start Main Window
my $MW = MainWindow->new();

#MainWindow Options
$MW->title('File Counter 1.0.8a GUI');
$MW->geometry("300x310");

# save data
my $data = {
dir_content => [],
count => '0',
base_dir => '',
};
my $precent = 0;

my $msgarea = $MW->Label(-borderwidth => 2, -relief => 'groove')
->pack(-side => 'bottom', -fill => 'x');
my $balloon = $MW->Balloon(-statusbar => $msgarea, -initwait => 150);


#Button Frame
my $LabelFrame = $MW->Frame()->pack(-side => 'left', -fill => 'both');

#Find Folder Button
my $Button1 = $LabelFrame->Button(-text => 'Find Folder', -command => \&opendirr)->pack();
$balloon->attach($Button1, -balloonmsg => "Find current folder",
-statusmsg => "Press the Button to locate a file in the folder you scaned to");

# Process Button
my $Button2 = $LabelFrame->Button(-text => 'Start Processing', -command => \&startcounting)->pack();
$balloon->attach($Button2, -balloonmsg => "Start Processing files",
-statusmsg => "Press the Button to Start processing.");

# Clear Button
my $clearl = $LabelFrame->Button(-text => 'Reset/Clear', -command => \&clear)->pack();
$balloon->attach($clearl, -balloonmsg => "Clear/Reset folder data",
-statusmsg => "Press the Button to clear the loaded folder data.");


#### Listbox
my $ProFrame = $MW->Frame()->pack(-fill => 'both');
my $list = $ProFrame->Scrolled('Listbox',
-selectmode => 'single',
-scrollbars => 'e')->pack(-fill => 'both');

$list->waitVariable(\$data->{count});
my $label_t1 = $ProFrame->Label(-text => "Found a total of ",)->pack(-side => 'left', -fill => 'x');
my $label_t = $ProFrame->Label(-textvariable => \$data->{count})->pack(-side => 'left', -fill => 'x');
my $label_t3 = $ProFrame->Label(-text => " File(s)",)->pack(-side => 'left', -fill => 'x');


#MainLoop
MainLoop();


sub clear
{

delete $data->{dir_contents};
$data->{count} = 0;
$data->{base_dir} = '';
$list->delete(0,'end');
#$data->{dir_contents};
$label_t->update;
}

sub startcounting
{
my ($dir) = @_;
my @TEMP;

my $count = 0;
open(CFILE,">$data->{base_dir}/MASTERFILE.txt");
foreach (@{$data->{dir_contents}}){
print CFILE "Total Files: ".$data->{count}."\n" if $count eq 0;
print CFILE "Location: ".$data->{base_dir}."\n\n" if $count eq 0;
$count++;
print CFILE "$_\n";
}
close CFILE;

my $response = $MW->messageBox(-message=>"File MASTERFILE.txt has been created and put in the folder you selected.",-type=>'ok');

open TEMP, "$data->{base_dir}/MASTERFILE.txt";
@TEMP = <TEMP>;
close TEMP;



my $response2 = $MW->messageBox(-message=>"Data Sent to printer....",-type=>'ok');
}

sub load_dir
{
my ($dir) = $_[0];
#$data->{count}=0;
$dir =~ /(\w\:\/.+\/)/;
my $new_d = lc($1);
$data->{base_dir} = $new_d;
#my $counter = 0;
opendir(DIR,"$new_d") || die("cant readdir $new_d\n");
while ((my $file = readdir(DIR))){
next if $file eq "." || $file eq "..";
if ($file){
$data->{count}++;
push @{$data->{dir_contents}}, $file;
$list->insert('end',"$file");
}else { next; }

}
}

sub opendirr
{
#this returns a file path but easy striped off file name
my $file = $MW->getOpenFile( );
&load_dir($file) if defined $file;
}



(This post was edited by wickedxter on Oct 2, 2011, 8:01 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives