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

Hi, I am trying to print all lines of a .dat file instead of just 1 random one. I have been trying to use a loop to print each line until the end of file. I am still quite new to Perl and am just learning OE Perl. Can anyone offer some input? Thanks! Here is what I am starting with:
#!/usr/bin/perl -wT use strict; use CGI; my $line = ""; my $part = ""; my $adfile = 'banners.dat'; open(FILE, $adfile) || die "Can't open $adfile"; #srand; # This chooses a random line, and puts that line into # the var $line. rand($.) < 1 && ($line = $_) while <FILE>; # This gets rid of the space at the end on each line chop $line; # Now you can split it my @part = split(/\|/,$line); # Close the file close(FILE); # Display the banner print "Content-type: text/html\n\n"; print "<font face=\"arial\" size=\"2\"><a class=\"banner\" href=\" +@part[0]\" target=\"_blank\"><img src=\"@part[1]\" width=\"@part[2]\" + height=\"@part[3]\" alt=\"@part[4]\" border=\"1\"><br>Click here to +visit my sponsor! </a></font>\n";

Replies are listed 'Best First'.
Re: How can I print all lines instead of 1 random?
by CheeseLord (Deacon) on Aug 20, 2001 at 04:39 UTC

    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

      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

Re: How can I print all lines instead of 1 random?
by blakem (Monsignor) on Aug 20, 2001 at 04:35 UTC
    I don't quite see how the @part array is dealt with when you are looking at the entire file. However, the code that picks a random line is:

    rand($.) < 1 && ($line = $_) while <FILE>;

    You could easilly change this to loop over the file, doing something with each line along the way:

    while (<FILE>) { my $line = $_; chomp($line); # note, 'chomp' is a safer version of 'chop': see +perldoc -f chomp; # do something with $line... }

    Hope this helps.

    -Blake

Re: How can I print all lines instead of 1 random?
by George_Sherston (Vicar) on Aug 20, 2001 at 13:49 UTC
    Or how about:
    my $adfile = 'banners.dat'; open(FILE, $adfile) || die "Can't open $adfile"; print "Content-type: text/html\n\n"; ## this line is the idiom that might save you some sweat: while ($line = <FILE>) { chop $line; my @part = split(/\|/,$line); print "<font face=\"arial\" size=\"2\"><a class=\"banner\" href=\" $ +part[0]\" target=\"_blank\"><img src=\"$part[1]\" width=\"$part[2]\" +height=\"$part[3]\" alt=\"$part[4]\" border=\"1\"><br>Click here to v +isit my sponsor! </a></font>\n"; }
    How many ways to do it are there? More than one, there are.

    § George Sherston
      That worked fine, thanks!