424,279 Members | 1,905 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,279 IT Pros & Developers. It's quick & easy.

How to print text file from hash table one line at a time

P: 74
I'm having difficulty trying to figure out how to print a text file from a hash table one line at a time. I have the text file read into the hash table and can print the text file all at once, but I can't seem to figure out how to do it one line at a time.

Here is what I'm trying to do: I want the user to be able to print the text file one line at a time by clicking a button to see the next line.

Example: If text_file1 first line is line1 and the second line is line2, I want line1 to print when the user clicks the submit button then when they click it again line2 will be printed. Any help would be appreciated. If you need more information or an explanation please ask.

Expand|Select|Wrap|Line Numbers
  1. #! /usr/local/bin/perl
  2.  
  3. BEGIN
  4. {
  5.         open(STDERR, ">&STDOUT");
  6.         select(STDERR); $| = 1;
  7.         select(STDOUT); $| = 1;
  8.         print qq{Content-type: text/html\n\n 
  9.         <title>Assignment #6 (CS Credit)</title>
  10.         <body text="white" bgcolor="blue">
  11.         <font face="arial" size="4">
  12.         <div align="center">};
  13. }               
  14.  
  15. my $url = '/~westj2/cgi-bin/assignment6_CS_credit5.cgi';
  16.  
  17. $buffer = $ENV{'QUERY_STRING'};
  18.  
  19. if($buffer eq "")
  20. {
  21.         &input;
  22.         exit;
  23. }       
  24.  
  25. @pairs = split(/&/, $buffer);
  26. foreach $pair(@pairs)
  27. {
  28.         ($name, $value) = split(/=/, $pair);
  29.  
  30.         $value =~ tr/+/ /;
  31.         $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  32.         $name =~ tr/+/ /;
  33.         $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  34.  
  35.         if($FORM{$name} eq "")
  36.  
  37.         if($FORM{$name} eq "")
  38.         {
  39.                 $FORM{$name} = $value;
  40.         }
  41.         else
  42.         {
  43.                 $FORM{$name} .= " " . $value;
  44.         }
  45. }
  46.  
  47. my $linenum = 1;
  48.  
  49. &hash;
  50.  
  51. my $counter;
  52.  
  53. if($FORM{'direction'} eq 'forward')
  54. {
  55.         $counter = 1;
  56. }
  57. else
  58. {       
  59.         $counter = $linenum - 1;
  60. }
  61.  
  62. &input;
  63. &output;
  64.  
  65. sub input
  66. {
  67.         print "<form method='get' action='$url'>";
  68.  
  69.         print "<b><font face='arial' color='white' size='3'><table
  70.         align='center'><tr><td><b>Select a file to scan: </td><td><select name='filename'>";
  71.  
  72.         if($FORM{'filename'} eq 'Seinfeld')
  73.         {
  74.                 my $filename = $FORM{'Seinfeld'};
  75.                 print "<option value='Seinfeld' SELECTED>Seinfeld</option>";
  76.                 print "<option value='Running_PRs'>Running PRs</option>";
  77.         }   
  78.         else
  79.         {
  80.                 $filename = $FORM{'Running_PRs'};
  81.                 print "<option value='Seinfeld'>Seinfeld</option>";
  82.                 print "<option value='Running_PRs' SELECTED>Running PRs</option>";
  83.         }
  84.  
  85.         print "</select></td></tr><br><tr><td><b><font face='arial' color='white' size='3'>";
  86.  
  87.         if($FORM{'direction'} eq 'forward')
  88.         {
  89.                 my $direction = $FORM{'forward'};
  90.                 print "<tr><td><b><font face='arial' color='white' size='3'><label for='forward'>Forward</label></td><td><input
  91. type='radio' name='direction' value='forward' CHECKED></td></tr><br>";
  92.                 print "<tr><td><b><font face='arial' color='white' size='3'><label for='reverse'>Reverse</label></td><td><input
  93. type='radio' name='direction' value='reverse'></td></tr><br>";
  94.         }
  95.         else
  96.         { 
  97.  
  98.         else
  99.         {
  100.                 $direction = $FORM{'reverse'};
  101.                 print "<tr><td><b><font face='arial' color='white' size='3'><label for='forward'>Forward</label></td><td><input 
  102. type='radio' name='direction' value='forward'></td></tr><br>";
  103.                 print "<tr><td><b><font face='arial' color='white' size='3'><label for='reverse'>Reverse</label></td><td><input 
  104. type='radio' name='direction' value='reverse' CHECKED></td></tr><br>";
  105.         }
  106.  
  107.         print "</table><p align='center'><input type='submit'
  108. name='submit' value='Submit'><input type='reset'
  109. value='Reset'></form>";
  110. }
  111.  
  112. sub hash
  113. {
  114.         open(file, "<$FORM{'filename'}") || die "Cannot open file: $!\n";
  115.  
  116.         my $line;
  117. #       my $linenum = 1;
  118.  
  119.         while($line = <file>)
  120.         {
  121.                 $hash{$linenum} = $line;
  122.                 $linenum++;
  123.         }
  124. }       
  125.  
  126. sub output
  127. {
  128.         print "$hash{$counter}";
  129.  
  130.         if($FORM{'direction'} eq 'forward')
  131.         { 
  132. #               $line = $linenum - 1;
  133.  
  134.                 $counter++;
  135.         }
  136.         else
  137.         {
  138.                 $counter--;
  139.         }
  140. }
  141.  
Jul 20 '09 #1
Share this Question
Share on Google+
11 Replies


KevinADC
Expert 2.5K+
P: 4,059
And you want to do this in a CGI script? First thing is to correct all syntax errors. Here is one:

Expand|Select|Wrap|Line Numbers
  1.         if($FORM{$name} eq "")
  2.  
  3.         if($FORM{$name} eq "")
You find the rest, if any.
Jul 20 '09 #2

P: 74
Sorry, I must have copied that line twice when I was copying and pasting it. Line 35 isn't supposed to be there. I think that's the only "error" that was in there. I wasn't getting any error messages.
Jul 20 '09 #3

numberwhun
Expert Mod 2.5K+
P: 3,503
@JWest46088
While that line may not have been there, Kevin is referring to syntax, which is important. Unless you tell it, Perl won't always complain about everything. So, right after the first line in your code put:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
Then, re-run your script and see what it tells you.

You need to be careful when you just copy code from somewhere else. Be sure you know what it does. Also, too many people don't follow standard, good coding practices.

Regards,

Jeff
Jul 20 '09 #4

P: 74
@numberwhun
This is my code. I meant when I was copying it from my editor to here, but it probably isn't good, standard coding practices. ;P

Any suggestions for my predicament?
Jul 20 '09 #5

Expert Mod 100+
P: 589
Why would you want to do this in a CGI environment?

You should be using the CGI module to process the form submission instead of doing it manually.

Loading the entire file into a hash when you only want a single line is probably the worst/slowest approach.

I haven't check your code to see if you're already doing this, but you need to update the form with the current line number and pass that back as a hidden field.

You really should be using css instead of those depreciated html tags (such as the font tag).
Jul 21 '09 #6

P: 74
@RonB
I know it may not be the best and most efficient code, but that is how I was taught to do it.

In one of my programs trying to accomplish this, I don't know if it's the one I posted, but I do have the line number as a hidden field inside the input sub. The line number isn't incrementing like I want it to though.
Jul 21 '09 #7

Expert Mod 100+
P: 589
There's nothing in your posted code that indicates that you're tracking the currently displayed line number.

Start by making the changes that have already been suggested, then post that new code and tell us where you need more help.
Jul 21 '09 #8

P: 74
@RonB
Here is the actually program:

http://web.cs.sunyit.edu/~westj2/cgi...CS_credit5.cgi

Here is the new code:

http://web.cs.sunyit.edu/~westj2/cgi...S_credit5_code

Please excuse all of the comments. I've been trying everything to figure this out. The hidden fields are on the bottom of the input sub. The thing that I need help with is displaying the file one line at a time when the user clicks the submit button. It only prints out the first line of the file and doesn't go any further. The counter must not be incrementing or something, but I can't figure out why.

Same code:

Expand|Select|Wrap|Line Numbers
  1. #! /usr/local/bin/perl
  2.  
  3. BEGIN
  4. {
  5.         open(STDERR, ">&STDOUT");
  6.         select(STDERR); $| = 1;
  7.         select(STDOUT); $| = 1;
  8.         print qq{Content-type: text/html\n\n
  9.         <title>Assignment #6 (CS Credit)</title>
  10.         <body text="white" bgcolor="blue">
  11.         <font face="arial" size="4">
  12.         <div align="center">};
  13. }
  14.  
  15. my $url = '/~westj2/cgi-bin/assignment6_CS_credit5.cgi';
  16.  
  17. $buffer = $ENV{'QUERY_STRING'};
  18.  
  19. if($buffer eq "")
  20. {
  21.     &input;
  22.     exit;
  23. }
  24.  
  25. @pairs = split(/&/, $buffer);
  26. foreach $pair(@pairs)
  27. {
  28.         ($name, $value) = split(/=/, $pair);
  29.  
  30.         $value =~ tr/+/ /;
  31.         $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  32.         $name =~ tr/+/ /;
  33.         $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  34.  
  35.         if($FORM{$name} eq "")
  36.         {
  37.                 $FORM{$name} = $value;
  38.         }
  39.         else
  40.         {
  41.                 $FORM{$name} .= " " . $value;
  42.         }
  43. }
  44.  
  45. my $linenum = 1;
  46. my $line;
  47. my $counter;
  48.  
  49. &hash;
  50.  
  51. #my $counter = $FORM{'counter'};
  52.  
  53. if($FORM{'direction'} eq "forward")
  54. {
  55.     $counter = 1;
  56. }
  57. else
  58. {
  59.     $counter = $linenum - 1;;
  60. }
  61.  
  62. #$olddirection = $FORM{'direction'};
  63. #$oldfilename = $FORM{'filename'};
  64.  
  65. print "exit hash $counter";
  66.  
  67. #&output;
  68. &input;
  69. &output;
  70.  
  71. #        if(($counter >= $linenum) || ($counter <= 0))
  72. #        {
  73. #        $counter++;
  74. #                &output;
  75. #        exit;
  76.         }
  77.  
  78. #&next;
  79.  
  80. #sub next
  81. #{
  82.  
  83. #if($FORM{'next'} eq 'check')
  84. #{
  85. #    &output;
  86. #}
  87. #}
  88. #&output;
  89. #&output;
  90. #&output;
  91. #}
  92.  
  93. sub input
  94. {
  95.     print "<form method='get' action='$url'>";
  96.  
  97.     print "<b><font face='arial' color='white' size='3'><table
  98.     align='center'><tr><td><b>Select a file to scan: </td><td><select name='filename'>";
  99.  
  100.         if($FORM{'filename'} eq 'Seinfeld')
  101.         {
  102.                 my $filename = $FORM{'Seinfeld'};
  103.                 print "<option value='Seinfeld' SELECTED>Seinfeld</option>";
  104.                 print "<option value='Running_PRs'>Running PRs</option>";
  105.         }
  106.         else 
  107.         {
  108.                 $filename = $FORM{'Running_PRs'};
  109.                 print "<option value='Seinfeld'>Seinfeld</option>";
  110.                 print "<option value='Running_PRs' SELECTED>Running PRs</option>";
  111.         }
  112.  
  113.         print "</select></td></tr><br><tr><td><b><font face='arial' color='white' size='3'>";
  114.  
  115.         if($FORM{'direction'} eq 'forward')
  116.         {
  117.                 my $direction = $FORM{'forward'};
  118.                 print "<tr><td><b><font face='arial' color='white' size='3'><label for='forward'>Forward</label></td><td><input type='radio' name='direction' value='forward' CHECKED></td></tr><br>";
  119.                 print "<tr><td><b><font face='arial' color='white' size='3'><label for='reverse'>Reverse</label></td><td><input type='radio' name='direction' value='reverse'></td></tr><br>";
  120.         }
  121.         else
  122.         {
  123.                 $direction = $FORM{'reverse'};
  124.                 print "<tr><td><b><font face='arial' color='white' size='3'><label for='forward'>Forward</label></td><td><input type='radio' name='direction' value='forward'></td></tr><br>";
  125.                 print "<tr><td><b><font face='arial' color='white' size='3'><label for='reverse'>Reverse</label></td><td><input type='radio' name='direction' value='reverse' CHECKED></td></tr><br>";
  126.         }
  127.  
  128.         print "</table><p align='center'><input type='submit' name='submit' value='Submit'><input type='hidden' name='counter' 
  129. value='$counter'><input type='hidden' name='linenum' value='$linenum'></form>";
  130. #<input type='hidden' name='olddirection' value='$olddirection'><input type='hidden' name='oldfilename' 
  131. #value='$oldfilename'><input type='hidden' name='counter' value='$counter'></form>";
  132.  
  133. #        my $line;
  134. #        my $linenum = 1;
  135.  
  136. #        open(file, "<$FORM{'filename'}") || die "Cannot open file: $!\n";
  137. #        while($line = <file>)
  138. #        {
  139. #                $hash{$linenum} = $line; 
  140. #                $linenum++;
  141. #        }
  142.  
  143. #print "<input type='checkbox' name='next' value='check'>";
  144. #print "<input type='checkbox' name='next' value='uncheck'>";
  145.  
  146.  
  147.     print "test";
  148.     print "<br>linenum: $linenum";
  149.     print "<br>line: $line";
  150.     print "<br>counter: $counter";
  151. }
  152.  
  153. sub hash
  154. {
  155.         open(file, "<$FORM{'filename'}") || die "Cannot open file: $!\n";
  156.  
  157. #    my $line;
  158. #    my $linenum = 1;
  159.     print "hash linenum: $linenum";
  160.  
  161.         while($line = <file>)
  162.         {  
  163.                 $hash{$linenum} = $line;
  164.                 $linenum++;
  165.         }
  166.     print "hash linenum: $linenum";
  167.  
  168.     for($linenum = 1; $linenum < 7; $linenum++)
  169.     {
  170.         print "hash: $hash{$linenum}";
  171.     }
  172. }
  173.  
  174. sub output       
  175. {
  176. #    print "<form method='get' action='$url'>";
  177.     print "entering output";
  178.  
  179.     print "line: $line<br>";
  180.     print "linenum: $linenum<br>";
  181.     print "counter: $counter<br>";
  182.  
  183.  
  184. #    $line = $linenum - 1;
  185.  
  186.     print "$hash{$counter}";
  187.  
  188.         if($FORM{'direction'} eq 'forward')
  189.         {                
  190. #            $line = $linenum - 1;
  191.  
  192.                 $counter++;
  193.         print "coutner: $counter";
  194.         }                
  195.         else                    
  196.         {                
  197.                 $counter--;
  198.         }
  199.  
  200. #    if(($counter >= $linenum) || ($counter <= 0))
  201. #    {
  202. #        &input;
  203. #    }
  204. }
  205.  
Jul 21 '09 #9

KevinADC
Expert 2.5K+
P: 4,059
When I try your new code this is the output:

Content-type: text/html


<title>Assignment #6 (CS Credit)</title>
<body text="white" bgcolor="blue">
<font face="arial" size="4">
<div align="center">Unquoted string "file" may clash with future reserved word at C:\PROGRA~2\PERLBU~1\debug\spbdtest.cgi line 158.
Unmatched right curly bracket at C:\PROGRA~2\PERLBU~1\debug\spbdtest.cgi line 79, at end of line
syntax error at C:\PROGRA~2\PERLBU~1\debug\spbdtest.cgi line 79, near "}"
syntax error at C:\PROGRA~2\PERLBU~1\debug\spbdtest.cgi line 154, near "}"
syntax error at C:\PROGRA~2\PERLBU~1\debug\spbdtest.cgi line 175, near "}"
syntax error at C:\PROGRA~2\PERLBU~1\debug\spbdtest.cgi line 207, near "}"
Execution of C:\PROGRA~2\PERLBU~1\debug\spbdtest.cgi aborted due to compilation errors.

So I really have no idea how you are even running that code since it still appears to have a few syntax errors.
Jul 21 '09 #10

Expert
P: 80
Ovid's CGI Course is a good tutorial about CGI Perl programming
Jul 21 '09 #11

numberwhun
Expert Mod 2.5K+
P: 3,503
@KevinADC
Sure, put the two pragmas that I mentioned at the beginning of your script, right after the shebang line and solve all of your syntax issues. Then, once they are out of the way and not bothersome, you can get down to any real errors in your code.

I wasn't mentioning them earlier to point out any type of bad coding, I was suggesting them for just this purpose. It is good coding practice to use them, that way you avoid all syntax issues and can see if your code runs right.

Regards,

Jeff
Jul 21 '09 #12

Post your reply

Sign in to post your reply or Sign up for a free account.