I assume that this is an extension to your question at Reading files sequentially. But I think you are heading down the wrong path in this case - why hard code all those numbers into your HTML? What if you want to add another image to your collection? What is you want to change which page displays a certain image?

A better way may be to force the cgi script to do the work of actually figuring out which page it is coming from (by the HTTP_REFERER variable, or by a unique number sent by the page, instead of a range) and which images to possibly display. Then you can easily change which images go to which pages instantly at one place - inside your script. This also reduces the problem of cached HTML pages that won't instantly reflect your changes like a cgi script would.

You can still number your images as above, but group them by page "number", for example:

## Read in all the images and numbers. There are probably ## not that many, so don't worry about the efficiency of it too much. @Images = &LoadImages(); ## Figure out which page it is coming from. For this example, ## let's say that each page (or even section of a page for ## multiple banners on one page (yuck!)) my $pagenumber = &GrabNumber(); ## Now just populate an hash of number strings. Since the page numbers + ## are just simply sequential in this example, we could ## have used an array, but the hash lines up ## neatly and allows us to easily change page "34" later ## without counting lines, or worrying about order. ## Each pagenumber gets it's own grouping of images. my %imagegroup = ( 1 => "1,2,5,7", 2 => "2,4", 3 => "*", 4 => "1,2,3", 5 => "4,8,10", ); ## For the above, page "1" gets images one, two, five, and seven, etc. +.. ## Note how pagenumber "3" is always going to get all images. ## Then just generate a random image. Again, because the ## list of images will probable be small, we don't worry ## too much about efficiency here (besides, it's early!) ## Default to "all images" my $grouping = $imagegroup{$pagenumber} || "*"; my $bail=0; { my $guess = int rand @Images; last if $grouping eq "*" or $bail++ > 1000; $grouping =~ /\b$guess\b/ or redo; } ## Now $Image[$guess] is the one to display...

Just one way to do it, but it does add flexibility, and makes it very easy to add or remove an image to a particular page, without mucking in HTML. :)


In reply to Re: picking x numbers of lines from a file by turnstep
in thread picking x numbers of lines from a file by damian

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.