damian has asked for the wisdom of the Perl Monks concerning the following question:

hi fella's, i'm a newbie so pls. forgive me for asking dumb questions. i want to randomly display images into HTML files. but what i want is to read the files sequentially not randomly. here is my data file that my CGI script reads.
1|/images/banners/internal/image1.gif|http://www.blah.com 2|/images/banners/internal/dumb.gif|http://www.dumb.com 3|/images/banners/internal/perl.gif|http://www.perl.com 4|/images/banners/internal/slashdot.gif|http://www.slashdot.org 5|/images/banners/internal/linux.gif|http://www.linux.org 5|/images/banners/internal/apache.gif|http://www.apache.org 6|/images/banners/internal/bsd.gif|http://www.bsd.org 7|/images/banners/internal/penguin.gif|http://www.penguin.com
now my script should run like this via IMG SRC tag.
<img src="/cgi-bin/showad.cgi?type=internal&begin=1&end=3"> <img src="/cgi-bin/showad.cgi?type=internal&begin=4&end=7">
the first IMG SRC tag runs the script showad.cgi and with the "begin" parameter having the value 1 and "end" parameter having the value "3" means that the script will pick the lines 1 to 3 from the above data file and print them randomly. same with the next IMG SRC having "begin" parameter with value 4 and "end" parameter 7 means that the script will begin at line 4 to 7 and randmly displays the image to the HTML file. i know you could help me on this guys. thanks in advance.

Replies are listed 'Best First'.
Re: picking x numbers of lines from a file
by turnstep (Parson) on Sep 19, 2000 at 14:14 UTC

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

Re: picking x numbers of lines from a file
by larsen (Parson) on Sep 19, 2000 at 13:35 UTC
    You could try this piece of code, in order to pick up a selection of the file. But, in this way, you actually read the entire file. What about using an array containing (index, image, url) tuples?
    (I think in this case you don't need a hash).
    #!/usr/bin/perl $from = 3; $to = 5; # There's the nice operator .., which has a useful # behaviour in scalar context while (<DATA>) { if (($from == $.) .. ($to == $.)) { print "$.: $_"; } } __DATA__ 1|/images/banners/internal/image1.gif|http://www.blah.com 2|/images/banners/internal/dumb.gif|http://www.dumb.com 3|/images/banners/internal/perl.gif|http://www.perl.com 4|/images/banners/internal/slashdot.gif|http://www.slashdot.org 5|/images/banners/internal/linux.gif|http://www.linux.org 5|/images/banners/internal/apache.gif|http://www.apache.org 6|/images/banners/internal/bsd.gif|http://www.bsd.org 7|/images/banners/internal/penguin.gif|http://www.penguin.com

    I hope this could be useful
    larsen
      Actually, the scalar range operator defaults to testing against equality with C<$.> So, more simply, the above code becomes:  ($from .. $to) && print while <DATA>
        From Perl 5.004 docs:
        If either operand of scalar ".." is a numeric literal, that operand is implicitly compared to the $. variable, the current line number.
        This is an excerpt from MacPerl documentation (I'm used to program on Macintosh). On my Linux box (with Perl 5.004 installed) the phrase is (I think):
        If eitrher operand ... is a constant
        Am I missing something?
        -- "Fatti non foste a viver come bruti"
RE: picking x numbers of lines from a file
by little (Curate) on Sep 19, 2000 at 21:05 UTC
    Hi damian, I dont't know how others feel, but I hate my browser cache being filled up with all the same images, due to the fact that they where inserted into the page like you do here with the IMG SRC. Why don't you use CGI and compose the src_path to the desired image and write that clean to the requested document? The browser will get it ore might even use the previous cahced version. And how does your script react when I say type=paid, would I get it then, even without payment? So far ..