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

Good Afternoon Monks! My question is simple. How do I add a correct paging function to my sub routine code posted below? Thanks in advance!

sub viewallrailcars { Title "View All Cars"; open(MYINPUTFILE, "cardata.txt"); my $viewlines = 5; $| = 1; my @lines = <MYINPUTFILE>; close (MYINPUTFILE); printf ("%-15s %-15s\n","====================="); printf ("%-15s %-15s\n","|CAR MODEL|CAR OWNER|"); printf ("%-15s %-15s\n","====================="); foreach (@lines) { chomp; print; my ($VcarModel, $VcarOwner) = split(":"); $VcarModel = "" if !defined($VcarModel); $VcarOwner = "" if !defined($VcarOwner); $. % $viewlines == 0 ? <> : print "\n"; my $format = " %-13s %0s\n"; printf ($format, $VcarModel, $VcarOwner); } print ("\n\n\nWHEN YOU ARE DONE VIEWING HIT RETURN: \n\n\n"); my $input = <STDIN>; $input = <STDIN> until defined $input; chomp($input); cls(); # Clears the screen car(); # Returns to the Car Menu } # End Subroutine

The command half works...it displays one line of data when enter is pressed the remaining data from the file is displayed. Not in the correct format.

Replies are listed 'Best First'.
Re: Adding Pages
by Not_a_Number (Prior) on Mar 19, 2014 at 21:06 UTC

    Not quite sure what you're trying to do. It would be helpful if you supplied (a small but representative sample of) your input data, and the output that you would expect from this.

    However, I can tell you that this line:

    $. % $lines == 0 ? <> : print "\n";

    is wrong.

    First, since you have read the whole file into an array (@lines), and then you iterate over this array in a foreach loop, you can't use the input line number ($.).

    Second, you use a variable $lines, which has not been initialised anywhere in your code. I imagine you mean $viewlines.

    Update: Corrected typo (s/$!/$./;). Thanks Laurent_R.

      Hello Not_A_Number
      The sample input data would be this:

      Chevy Tahoe:Bob
      Ford Escape:Don
      Chevy Malibu:Chris

      Say I just want the first two lines and then page to the final line...of course there will be several input lines..the out put is I would like to page through the data instead of all the data show up in the console screen and have to scroll down to view everything. I hope I explained this thoroughly. Thanks in advance.

Re: Adding Pages
by Laurent_R (Canon) on Mar 19, 2014 at 22:43 UTC
    Hmm, not sure, but maybe the format and write functions are what you are after.

      That would mean I would have to rewrite quite a few subroutines. Something more along the lines of display first ten lines of the file, then the second ten lines and so on by using the enter/return key or any other keystroke for that matter...keeping the same header and footer and so on. I'm on the right track I think.

        You are not reading from a file, so $. does not change. Either read from the input when printing the lines, or use an index to the array instead.

        Here's how I modified your code:

        #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use v5.10; sub view_all_railcars { say "View All Cars"; *MYINPUTFILE = *DATA{IO}; # To simulate the input. my $viewlines = 4; print "=====================\n"; print "|CAR MODEL|CAR OWNER|\n"; print "=====================\n"; while (<MYINPUTFILE>) { chomp; my ($VcarModel, $VcarOwner) = split /:/; $_ //= q() for $VcarModel, $VcarOwner; if ($. > 1 and $. % $viewlines == 1) { <>; } else { print "\n"; } my $format = " %-13s %0s\n"; printf $format, $VcarModel, $VcarOwner; } print ("\n\n\nWHEN YOU ARE DONE VIEWING HIT RETURN: \n\n\n"); do 1 until defined <>; # Do not end on CTRL+D say 'cls'; say 'Menu'; } view_all_railcars(); __DATA__ Ford|Philip Renault|Renee Citroen|Cyrill Mercedes|Mike Chrysler|Chris Bentley|Brian BMW|Brittney Chevrolet|Shirley Skoda|Stanley
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ