Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hi jimhenry,

I agree with jethro that the best way to do this is to read the list of files in the program.  You shouldn't have to specify STDIN in your Term::ReadKey calls, either.  Also, consider putting "$| = 1" at the beginning of the program to flush STDOUT.

Here's a small subroutine which will let you avoid using File::Find and just get the files ending in ".txt":

sub find_all_textfiles { my ($dir) = @_; my $fh = new IO::File; opendir($fh, $dir) or die "Failed to read dir '$dir' ($!)\n"; my @files = readdir($fh); closedir $fh; my @found = ( ); foreach my $fname (@files) { next if $fname eq '.' or $fname eq '..'; my $path = "$dir/$fname"; if (-f $path and $path =~ /[.]txt$/) { push @found, $path; } elsif (-d $path) { push @found, find_all_textfiles($path); } } return @found; }

Of course this will require that you read each file individually, and somehow store their contents.

If it matters which file the text came from, you could save the lines from each file in a separate array reference, and then return a reference to those array references (note my use of Data::Dumper, which is invaluable for visualizing your data!):

use strict; use warnings; use Data::Dumper; use IO::File; use Term::ReadKey; use Time::HiRes qw(time); $| = 1; my @files = find_all_textfiles("."); my $a_text = read_files(@files); printf "Results of reading [@files] => %s\n", Dumper($a_text); sub read_files { my (@files) = @_; my $a_text = [ ]; foreach my $fname (@files) { print "Reading '$fname' ...\n"; my $fh = new IO::File($fname) or die "Can't read '$fname' ($!) +\n"; chomp(my @text = <$fh>); push @$a_text, [ @text ]; } return $a_text; }

If you don't care which text came from which file, just throw it all in one big array:

use strict; use warnings; use Data::Dumper; use IO::File; use Term::ReadKey; use Time::HiRes qw(time); $| = 1; my @files = find_all_textfiles("."); my @text = read_files(@files); printf "Results of reading [@files] => %s\n", Dumper([ @text ]); sub read_files { my (@files) = @_; my @text = ( ); foreach my $fname (@files) { print "Reading '$fname' ...\n"; my $fh = new IO::File($fname) or die "Can't read '$fname' ($!) +\n"; chomp(my @lines = <$fh>); push @text, @lines; } return @text; }

In fact, in the last example, you could even combine the finding of files with the saving of each file's text, to create a single subroutine.  I'm using a nifty trick here, which is to pass the array reference $a_text which is the aggregate of all text in each recursive call to read_textfile_lines; only at the top level is it undefined (but in that case, you initialize the array ref with:  $a_text ||= [ ];):

use strict; use warnings; use Data::Dumper; use IO::File; use Term::ReadKey; use Time::HiRes qw(time); $| = 1; my $a_text = read_textfile_lines("."); printf "Text from ALL textfiles in current dir: %s\n", Dumper($a_text +); sub read_textfile_lines { my ($dir, $a_text) = @_; $a_text ||= [ ]; my $fh = new IO::File; opendir($fh, $dir) or die "Failed to read dir '$dir' ($!)\n"; my @files = readdir($fh); closedir $fh; foreach my $fname (@files) { next if $fname eq '.' or $fname eq '..'; my $path = "$dir/$fname"; if (-f $path and $path =~ /[.]txt$/) { $fh = new IO::File($fname) or die "Can't read '$fname' ($! +)\n"; chomp(my @lines = <$fh>); push @$a_text, @lines; close $fh; } elsif (-d $path) { read_textfile_lines($path, $a_text); } } return $a_text; }

Now you're ready to call some subtroutine process_text (or whatever), passing the ref to the array of all text $a_text.  It will do something like:

sub process_text { my ($a_text) = @_; while (1) { my $key; my $wait_until = time + 3; while ( time < $wait_until ) { ReadMode 3; $key = ReadKey( -1 ); if ( defined $key ) { print "keystroke $key\t"; } ReadMode 0; } print_some_random_text($a_text); } } sub print_some_random_text { my ($a_text) = @_; # Replacing the next line with meaningful code is left as # an exercise for the OP. ## print "[Debug]\n"; }

As you see, I've left for you the fun of deciding what to print out in the subroutine print_some_random_text, as well as adding code for trapping relevant keys from the user in the subroutine process_text.  Good luck!


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

In reply to Re^4: Problem with ReadKey by liverpole
in thread Problem with ReadKey by jimhenry

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-19 23:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found