in reply to How can I print all lines instead of 1 random?

Aye, input I can give. In order to display every line of the file, you're gonna need the whole file sucked in to an array if you want to output the lines randomly. If I'm reading your request right, you want to output all these banners in random order. If that's the case, try something like this:

#!/usr/bin/perl -wT use strict; my $adfile = 'banners.dat'; open FILE, "< $adfile" or die "Can't open $file: $!\n"; my @lines; splice(@lines, rand $., 0, $_) while <FILE>; close FILE; chomp @lines; print "Content-Type: text/html\n\n"; for my $line (@lines) { my @parts = split '\|', $line; print <<__End__; <font face="arial" size="2"> <a href="$part[0]" ... . . . </font> __End__ }

I should mention a slight problem with your syntax: @part[0] should really be $part[0] (although in truth, it'll still work, but might cause trouble down the road with context issues). Also, the code is untested, so if there's any problems, /msg me and I'll try to help you out. And ++ to MeowChow for giving me the idea for the randomization.

Update: As tilly mentions below, there are more efficent methods of randomizing your output, and if that's a concern, you'd do well to check out the links he provides. But it's probably nothing to worry about until you get a large number of ads in your .dat file.

His Royal Cheeziness

Replies are listed 'Best First'.
Re (tilly) 2: How can I print all lines instead of 1 random?
by tilly (Archbishop) on Aug 20, 2001 at 05:50 UTC
Re: Re: How can I print all lines instead of 1 random?
by ginocapelli (Acolyte) on Aug 20, 2001 at 07:22 UTC
    Thanks for the response. I can't get your suggestion to work. I don't want to print out all lines randomly, I would like each line printed one at a time, in order they are listed in the banners.dat file. I keep track of my sponsors in the banners.dat file including their link|image|width|height|alt tags I use the script (the one I posted) to show a random banner at the top of each page. I want to be able to show all my current sponsors on a separate page. Hope that makes sense.

      Okay, in that case, my code should still work about the same way... just change the splice line to this:

      @lines = <LINE>;

      And inside the for loop, you'll want to just print out the sponsors' links (or images, or whichever field(s) you want to print out), as opposed to printing out the complete set of information for each sponsor. If that fails for some other reason (say, "Cheesy wrote bad code") then tell me what errors you're getting and I'll give it another shot. Hope that helps!

      Note: If this file is big, you'll likely want to process each one individually with a while loop, instead of a for.

      His Royal Cheeziness