473,732 Members | 2,171 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

74 New Member
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 4212
KevinADC
4,059 Recognized Expert Specialist
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
JWest46088
74 New Member
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 Recognized Expert Moderator Specialist
@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
JWest46088
74 New Member
@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 Recognized Expert Moderator Contributor
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
JWest46088
74 New Member
@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 Recognized Expert Moderator Contributor
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
JWest46088
74 New Member
@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 Recognized Expert Specialist
When I try your new code this is the output:

Content-type: text/html


<title>Assignme nt #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\PER LBU~1\debug\spb dtest.cgi line 158.
Unmatched right curly bracket at C:\PROGRA~2\PER LBU~1\debug\spb dtest.cgi line 79, at end of line
syntax error at C:\PROGRA~2\PER LBU~1\debug\spb dtest.cgi line 79, near "}"
syntax error at C:\PROGRA~2\PER LBU~1\debug\spb dtest.cgi line 154, near "}"
syntax error at C:\PROGRA~2\PER LBU~1\debug\spb dtest.cgi line 175, near "}"
syntax error at C:\PROGRA~2\PER LBU~1\debug\spb dtest.cgi line 207, near "}"
Execution of C:\PROGRA~2\PER LBU~1\debug\spb dtest.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

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

Similar topics

7
22332
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 a hash map so I can access the file information in constant time, without having to iterate over a (possibly very large) list of files. As far as I know, std::map does not hash its entries (I guess it takes O(nlogn) time to find an entry)....
19
5697
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 a array, and then looping through that array looking for duplicates, removing them, and then writing to another file. This seems a very long and drawn out way of doing this to me, and also I
11
3616
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 million lines. b) I need to store the contents into a unique hash. c) I need to then sort the data on a specific field. d) I need to pull out certain fields and report them to the user.
0
2394
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 D 'Delete
21
3217
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 (i.e. the get_hash function) is borrowed from various snippets I found on the net. Thee free function could probably need some love. I have been thinking about having a second linked list of all entries so that the cost of freeing is in proportion to...
21
3898
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 applies in this case though: #include <limits.h> unsigned foo(void) { static const union { unsigned char str;
2
9975
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 printer settings to print. I wanted the print preview to appear the same for all users (i.e. a default page size of 8.5x14 (legal) and portrait mode). Many users have different printers as their default (plotters etc..) and I found that various...
139
14197
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
2436
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 file field separated by commas or not. I want to pring each line that has an actual date to the right of the word 'DATE:' if it doesnt have a date do not print that line. Then the next thing i want my perl program to do is print each line that has...
0
8774
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9235
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9181
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6735
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.