How to print text file from hash table one line at a time | Member | | Join Date: Sep 2006
Posts: 68
| |
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. |  | Expert | | Join Date: Jan 2007 Location: Southern California USA
Posts: 4,091
| | | re: How to print text file from hash table one line at a time
And you want to do this in a CGI script? First thing is to correct all syntax errors. Here is one: - if($FORM{$name} eq "")
-
-
if($FORM{$name} eq "")
You find the rest, if any.
| | Member | | Join Date: Sep 2006
Posts: 68
| | | re: How to print text file from hash table one line at a time
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.
|  | Site Moderator | | Join Date: May 2007 Location: New Hampshire
Posts: 2,572
| | | re: How to print text file from hash table one line at a time Quote:
Originally Posted by JWest46088 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. 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: -
use strict;
-
use warnings;
-
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
| | Member | | Join Date: Sep 2006
Posts: 68
| | | re: How to print text file from hash table one line at a time Quote:
Originally Posted by numberwhun 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: -
use strict;
-
use warnings;
-
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 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?
| | Member | | Join Date: Jun 2009
Posts: 55
| | | re: How to print text file from hash table one line at a time
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).
| | Member | | Join Date: Sep 2006
Posts: 68
| | | re: How to print text file from hash table one line at a time Quote:
Originally Posted by RonB 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). 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.
| | Member | | Join Date: Jun 2009
Posts: 55
| | | re: How to print text file from hash table one line at a time
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.
| | Member | | Join Date: Sep 2006
Posts: 68
| | | re: How to print text file from hash table one line at a time Quote:
Originally Posted by RonB 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. 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: |  | Expert | | Join Date: Jan 2007 Location: Southern California USA
Posts: 4,091
| | | re: How to print text file from hash table one line at a time
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.
| | Newbie | | Join Date: Jan 2008 Location: Moldova
Posts: 7
| | | re: How to print text file from hash table one line at a time Ovid's CGI Course is a good tutorial about CGI Perl programming
|  | Site Moderator | | Join Date: May 2007 Location: New Hampshire
Posts: 2,572
| | | re: How to print text file from hash table one line at a time Quote:
Originally Posted by KevinADC 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. 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
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,467 network members.
|