Hi, I modified your code to use threads, and I used numbers1..4 as the filenames, which I filled with
#!/usr/bin/perl use warnings; use strict; my $file = shift or die; open (FH,">$file") or die; for(1..1000){ my $first = sprintf('%03d', int rand 999); my $second = sprintf('%03d', int rand 999); my $third = sprintf('%04d', int rand 9999); print FH "$first-$second-$third\n"; }
for testing.

Now here is a way to search them for a single number, which stops all the threads when a number is found and reports the line number. Your Tk code could use some minor fixing, but that is up to you. There is also alot of code cleanup and opportunities for ingenious juggling of the return data....like search certain threads only for certain prefixes, or looking for the same number on multiple lists. But I made this as simple as I could to demonstrate the basics... like the timer to watch the shared variables, and how to cleanup the threads when exiting. Also you will need to work out proper messaging and resetting the threads after an empty search

#!/usr/bin/perl use warnings; use strict; use Tk; use Tk::Dialog; use threads; use threads::shared; my @sfiles = qw(numbers1 numbers2 numbers3 numbers4); # make threads first before any tk code my %shash; my %thash; my $numworkers = scalar @sfiles; my $searchnum; share $searchnum; $searchnum = '002-339-7473'; foreach my $dthread(1..$numworkers){ share ($shash{$dthread}{'go'}); share ($shash{$dthread}{'die'}); share ($shash{$dthread}{'file'}); share ($shash{$dthread}{'return'}); $shash{$dthread}{'go'} = 0; $shash{$dthread}{'file'} = shift @sfiles; $shash{$dthread}{'die'} = 0; $shash{$dthread}{'return'} = ''; $thash{$dthread}{'thread'} = threads->new(\&work,$dthread); } my $count = 1; my $mw = MainWindow->new; my $c = $mw->Canvas( -width =>400, -height=>200, -background => "white"); $c->pack; ############################Main window my $quit = $mw->Button( -text => "Quit", -padx => "8", -command => \&Quit); $quit->place(-x => 350, -y => 180); ###########################adds the quit button my $Find = $mw->Button( -text => "Search", -command => \&Search); $Find->place(-x => 300, -y => 180); ################################################ my $lab = $mw->Label ( -text => "Enter Phone Number", -background => "white" ); $lab->place(-x => 5, -y => 50); ##############################Label my $Entry = $mw->Entry( -textvariable => \$searchnum, -background => "white" ); $Entry->place(-x => 5, -y => 68); ##################################Entry my $text = $mw->Text( -height => 5, -width => 55, -wrap => 'char' ); $text->place(-x => 5, -y => 90); ################################# my $labWarning = $mw->Label ( -text => 'Example XXX - XXX - XXXX', -background => "white" ); $labWarning->place(-x => 140, -y => 68); my $index = 1; ####################################### sub Quit { my $d = $mw->Dialog( -text => "Are you sure you want to quit?", -buttons => ["Yes", "No"]); my $answer = $d->Show(); close(UNLOCK); #cleanup threads if($answer eq "Yes"){ for(1..$numworkers){ $shash{$_}{'die'} = 1; $thash{$_}{'thread'}->join; } exit; } } sub Search { my $found = 0; for(1..$numworkers){ $shash{$_}{'go'} = 1; } #setup a timer to watch for returns thru shared variables my $timer; # declare outside of block so you can kill it in callback $timer = $mw->repeat(10,sub{ for(1..$numworkers){ if( length $shash{$_}{'return'} > 0){ if( $shash{$_}{'return'} > 0 ){ my $message = "thread $_ detected number at line +$shash{$_}{'return'} \n"; print "$message\n"; $text->insert('end',"$message\n"); } #stop other threads for(1..$numworkers){ $shash{$_}{'return'} = 0 } #stop timer $timer->cancel; } } }); } MainLoop; ###################################################################### +# sub work{ my $dthread = shift; $|++; my $file = $shash{$dthread}{'file'}; open(FH,"< $file") or die "$!\n"; print "thread $dthread created\n"; while(1){ if($shash{$dthread}{'die'} == 1){ goto END }; if ( $shash{$dthread}{'go'} == 1 ){ seek FH, 0, 0; #reset to top $shash{$dthread}{'return'} = ''; print "thread $dthread going\n"; while(my $line = <FH>){ chomp $line; if($line eq $searchnum){ $shash{$dthread}{'return'} = __LINE__ ; print "$dthread found $shash{$dthread}{'return'}\ +n"; goto RESET; } if($shash{$dthread}{'go'} == 0){goto RESET} if($shash{$dthread}{'die'} == 1){ goto END }; + } RESET: $shash{$dthread}{'go'} = 0; #turn off self before returni +ng print "thread $dthread going back to sleep\n"; }else { select(undef,undef,undef,.1) } # tenth second sleep, can be + smaller } END: }

I'm not really a human, but I play one on earth. Cogito ergo sum a bum

In reply to Re: Searching text files by zentara
in thread Searching text files by SteveS832001

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.