473,416 Members | 1,536 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

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

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
11 4175
KevinADC
4,059 Expert 2GB
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
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
3,509 Expert Mod 2GB
@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
@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
RonB
589 Expert Mod 512MB
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
@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
RonB
589 Expert Mod 512MB
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
@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
4,059 Expert 2GB
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
chorny
80 Expert
Ovid's CGI Course is a good tutorial about CGI Perl programming
Jul 21 '09 #11
numberwhun
3,509 Expert Mod 2GB
@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

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

Similar topics

7
by: Matthias Käppler | last post by:
Hi, I need to store objects of a FileInfo class containing information about a specific file. I can uniquely identify these files by their serial number. I thought it would be a good idea to use...
19
by: Materialised | last post by:
Hi everyone, What I am wanting to do, is to copy, a simple plain text file, to another file, but omitting duplicate items. The way I thought of doing this, involved copying all the items into...
11
by: hoopsho | last post by:
Hi Everyone, I am trying to write a program that does a few things very fast and with efficient use of memory... a) I need to parse a space-delimited file that is really large, upwards fo a...
0
by: richardkreidl | last post by:
I have the following hash script that I use to compare two text files. 'Class Public Class FileComparison Public Class FileComparisonException Public Enum ExceptionType U 'Unknown A 'Add...
21
by: Johan Tibell | last post by:
I would be grateful if someone had a minute or two to review my hash table implementation. It's not yet commented but hopefully it's short and idiomatic enough to be readable. Some of the code...
21
by: Hallvard B Furuseth | last post by:
Is the code below valid? Generally a value must be accessed through the same type it was stored as, but there is an exception for data stored through a character type. I'm not sure if that...
2
by: Brad Pears | last post by:
I have a vb.net 2005 application and am using the print preview screen. This screen has a printer icon on it that the user can use to print the document currently being viewed. It uses the default...
139
by: ravi | last post by:
Hi can anybody tell me that which ds will be best suited to implement a hash table in C/C++ thanx. in advanced
3
by: itdaddy | last post by:
hey perl gurus! i am new to this forum cause i need help. I have done many scripts. but i want to use perl to do this: What I want to do is this. I have a QRP file that I can convert to a txt...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.