dubl1n has asked for the wisdom of the Perl Monks concerning the following question:

Hi folks - I am currently learning perl, self-teaching while I do my regular job, and as such I am working on a perl script where I am printing an array to the screen with a simple: print @cmd; # prints array command But the output from that array is about 200+ lines and I would like to put in some way to pause the output to about 20 or so lines so they can be viewed. So essentially putting in a command like: print "Please press a key to continue."; <STDIN>; But I am not sure how to get the functionality of it working and make is so it stops around 20 lines of output and then continues when you hit a key, like space or something. Thanks for the help, loving perl so far and hope it takes me far in my work. Dub
  • Comment on pause output when printing an array to the screen

Replies are listed 'Best First'.
Re: pause output when printing an array to the screen
by brx (Pilgrim) on Apr 23, 2012 at 15:57 UTC
    more or less ?!

    Or with Term::ReadKey:
    #!usr/bin/perl-w use strict; use Term::ReadKey; my @cmd = (0..200); $|=1; my $i=0; while (1){ print join "\n",grep {!/^$/} @cmd[$i*20 .. ((++$i*20)-1)],''; last if (20*$i > $#cmd); warn "\nPlease press a key to continue.\n"; my $key; while (not defined ($key = ReadKey(-1))) { } }
Re: pause output when printing an array to the screen
by Anonymous Monk on Apr 23, 2012 at 15:46 UTC
    Pipe the output into a terminal pager, e.g.:

    perl theprogram | less

    Trying to reinvent that particular wheel in Perl is not a thing I care about much.

Re: pause output when printing an array to the screen
by toolic (Bishop) on Apr 23, 2012 at 15:47 UTC
    Here is a time-based version using perldoc -f sleep
    use warnings; use strict; my @cmd = 1 .. 200; my $i = 0; for (@cmd) { print "$_\n"; $i++; sleep 2 if $i % 20 == 0; }

    See also:

Re: pause output when printing an array to the screen
by zentara (Cardinal) on Apr 23, 2012 at 16:12 UTC
    Here is another one. I would use an event-loop for this, but a while loop works.
    #!/usr/bin/perl use warnings; use strict; use Term::ReadKey; my $char='q'; my @array = ('aa' .. 'zz'); my $print_go = 1; my $ch; while(1){ ReadMode ('cbreak'); if (defined ( $ch = ReadKey(-1))){ #input was waiting and it was $ch print "$ch\n"; if ($ch eq $char){exit(0)}; if ($ch eq ' '){ $print_go = 0 }; #space stops print if ($ch eq 'g'){ $print_go = 1 }; if ($ch eq 'x'){ exit }; print "$ch\n"; }else{ # no input was waiting #your program goes here # print "############################\n"; my $val = shift @array; if( $print_go ) { print "$val\n" } select(undef,undef,undef,.1); } ReadMode ('normal'); # restore normal tty settings }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: pause output when printing an array to the screen
by stevieb (Canon) on Apr 23, 2012 at 16:09 UTC

    Here's a potential solution using the each() for arrays feature in 5.12:

    #!/usr/bin/perl use warnings; use strict; use 5.12.0; my @array = ( 'aa'..'zz' ); while ( my ( $i, $elem ) = each @array ){ if ( $i != 0 && $i % 20 == 0 ){ say "Hit ENTER to continue..."; <STDIN>; } say $elem; }

    If you don't have perl v5.12+, you'll need to use an external iterator:

    my $i = 0; for my $elem ( @array ){ if ( $i != 0 && $i % 20 == 0 ){ say "Hit ENTER to continue..."; <STDIN>; } say $elem; $i++; }
Re: pause output when printing an array to the screen
by dubl1n (Initiate) on Apr 23, 2012 at 17:26 UTC
    Thanks for the responses, a lot of great things for me to try in my script. Thanks again!! Dub