Re: Array contains too much data

From: Matthew Braid (mb_at_uq.net.au.invalid)
Date: 11/26/03


Date: Wed, 26 Nov 2003 14:13:59 +1000

Robert TV wrote:

> Hi. I have a small perl script that opens a file, collects and assigns each
> line of the file to an array, and then displays the data to the screen. This
> script is working just fine, but is starting to get difficult to manage ...
> you see, the data file now contains about 1,000 lines of data and it's
> taking very long for perl to parse the data, assemble it into html and then
> show in my browser (obviously it was very quick when the data file only had
> 10 enties total) Here is the jist of the script:
>
> ###### Start Script Example ######
>
> #!/usr/bin/perl
>
> open (FH, "<$user/datafile.db") or die "Can't open: $!";
> @entires=<FH>;
> close(FH);
>
> print "Content-type: text/html \n\n";
> print <<HTML;
> <html>
> <head>
> <title>Database Results</title>
> </head>
> <body>
> <table>
> HTML
>
> # DISPLAY DATABASE TABLES
> chomp @entires;
> foreach $entry(@entires) {
> print <<HTML;
> <tr>
> <td width="42" height="19">Entry</td>
> <td width="279" height="19">$entry</td>
> </tr>
> PRINTHTML
> }
> print <<HTML;
> </table>
> </body>
> </html>
> HTML
> exit;
>
> ###### End Script Example ######
>
> The database now at 1000 entires it takes very long to essemble and print to
> screen, and it's bogging down my browser because the HTML is too long. Now,
> I start to think ... I need a way to only show 20 or 30 results at a time,
> and have "next results" or something similair ... kinda like search engines
> do. So in an ideal world, I run the script and it only shows the first 30
> lines of the array and has links to more results ... like this:
>
> Showing 0-30 ---back 1 2 3 4 5 6 next #something like this.
>
> I'm posting this inquiry because I have no idea how I would approach or
> create such a feature, and I'm hoping that maybe a few of you out there
> might be able to point me in the right direction, maybe there are some
> tutorials, code snippets etc etc. I appriciate you taking time to read my
> post! TIA
>
> Robert TV
>
>

First off - I hope the line above consisting entirely of PRINTHTML was a
  typo - it should have been HTML.

Secondly, you're going to have to accept parameters of what the current
display length is (say 30 for 30 items per page) and what offset is
required (30 would mean page 2 for a 30-items-per-page display length,
while 25 would mean someones playing with your URL :) ). You also need
to know how many entries are in your 'database' (although this should
_NOT_ be a parameter - figure it out yourself). This means using CGI
stuff. Read up on the CGI module (perldoc CGI). Your URL (using the GET
method at least) will end up looking something like:

http://www.example.com/show_entries.pl?offset=60&page_size=30

The back and next links should point to offsets that match the previous
and next page (if the current request is for offset 90 with a page_size
of 30, back should be a link to offset 60 with a page_size of 30 and
next should be a link to offset 120 with a page_size of 30 - remember
that there may not be a next or prev page though!). Since CGI is
stateless you can't tell what the _current_ page is unless you add
another parameter, and its easy enough to do without that.

There are a lot of traps to CGI programming since you have no control
over the user possibly typing in their own parameters to try and break
your code.

Of course, there is still one little problem - your 'database' is just a
flat text file. Its always going to be slow for any decent sized file.
Unless the entries are fixed-length you can't just skip the appropriate
part of the file to get entries at a given offset - you have to read
every line in the file up to that offset and count the newlines. You may
want to consider a proper database.

MB



Relevant Pages

  • Re: The crazy encryption madmans codebook
    ... the actual identifier in the database '12288. ... Just note that if offset multiplies to 0 then 0+12288=12288 it is the ... plaintext sentence you will get another offset. ... All you're doing is attempting to build a not very good chaining mode. ...
    (sci.crypt)
  • Re: The crazy encryption madmans codebook
    ... I do not see how *telephone* always encodes to anything particular ... the actual identifier in the database '12288. ... plaintext sentence you will get another offset. ... All you're doing is attempting to build a not very good chaining mode. ...
    (sci.crypt)
  • Re: The crazy encryption madmans codebook
    ... encoded to any word in the database. ... Why would it not be possible, the offset number is just an integer ... where each entry have an index, realworld word or phrase and a madman ... would call *offset keys* an offset key is used to encode *one and just ...
    (sci.crypt)
  • Re: The crazy encryption madmans codebook
    ... the actual identifier in the database '12288. ... telephone=a bad smell' ... plaintext sentence you will get another offset. ... All you're doing is attempting to build a not very good chaining mode. ...
    (sci.crypt)
  • Re: JEditorPane and HTML offsets
    ... e.g. in the HTML document (not strict HTML but still ... "test" starts at offset 6 in the HTML - but in the JEditorPane only ...
    (comp.lang.java.gui)