in reply to picking x numbers of lines from a file

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. :)