#!/usr/bin/perl $filename ="reklama.dat"; $MAXLINES=2; # number of lines of ad content $MAXCHAR =35; # numbers of characters per a line of ads # This program is written by M. Verbitsky under GNU Public License # version 1.01, August 2000 # usage: # (for 3 lines of slogans) $maxlines = $ENV{'QUERY_STRING'}; if ($maxlines>0) {$MAXLINES=$maxlines;} ######################################## open(REKLAMA, "< $filename") || die ("couldn't open $filename: $!\n"); @filecontents =; close(REKLAMA); # format reklamy: # URL # slogan # [optional] coefficient (naskol'ko chasto ego puskat') # slogan # [optional] coefficient # slogan... # [optional] coefficient # URL # (etc.) $wnumber=0; # weighted number (with coefficients) $cont_slogan=(); $cont_url=(); $cont_coeff=(); $url="http://imperium.lenin.ru"; # parsing the ads list while (@filecontents>1) { comments_remove(); $mightbe_url = ($filecontents[0]); $test = substr ($mightbe_url, 0, 7); if ("$test" eq "http://") { $url=$mightbe_url; shift(@filecontents); comments_remove(); } $slogan=shift(@filecontents); comments_remove(); $mightbe_coeff=$filecontents[0]; if ($mightbe_coeff != 0) # esli ehto ne stroka, a nomer { $coeff=$mightbe_coeff; shift(@filecontents); } else {$coeff =1;} # modify the global parameters $wnumber += $coeff; #creating contents list push(@cont_slogan, $slogan); push(@cont_url, $url); push(@cont_coeff, $coeff); } # we constructed three lists of ad contents # printing the results of parsing -- commented below # print @cont_slogan; print @cont_url; print @cont_coeff; #$number=@cont_slogan; print "\n\nthe number of slogans is $number\n"; #$number=@cont_url; print "the number of urls is $number\n"; #$number=@cont_coeff; print "the number of coeff is $number\n"; #print "all three numbers should be equal!\n"; #print "the total coefficient is $wnumber\n"; @reklama=(); $numlines =0; while ($numlines < $MAXLINES && @cont_slogan>0) { my $line; # print "running oneline"; $line=oneline(); # print "$line"; push(@reklama, $line); ++$numlines; } print "Content-type:text/html; charset=koi8-r\n\n"; print @reklama; ########################### ##### ROUTINES ######### # creating just one line of adverts, returning this line sub oneline() { $line =""; $razdelitel =""; $current_line_length =0; while (@cont_slogan && $current_line_length<$MAXCHAR) { choose(); # now removing the trailing whitespace $url =~ s/ +$//g; $slogan =~ s/ +$//g; $current_line_length += length($slogan); $line = $line.$razdelitel."".$slogan.""; $razdelitel=" | \n"; } $line = $line."
\n"; return($line); } # function choose determines the number # of the ad and removes the corr. listing from the three lists # it writes its results in $url and $slogan sub choose () { $rnd = rand($wnumber); $choice =0; $test=0; @coeff_list= @cont_coeff; while ($test+@cont_coeff[$choice] < $rnd && defined(@cont_coeff[$choice])) { $test+= @cont_coeff[$choice]; #print "test is $test, choice is $choice\n"; ++$choice; } if (!defined(@cont_coeff[$choice])) {$choice=@cont_coeff;} # sanity check $wnumber -= @cont_coeff[$choice]; #total coefficient is decreased $slogan=splice(@cont_slogan, $choice, 1); $url=splice(@cont_url, $choice, 1 ); splice(@cont_coeff, $choice, 1); #$huj =@cont_coeff; #print "wnumber =$wnumber, rnd is $rnd, choice is $choice, test is $test, cont_coeff is $huj long\n"; #print "slogan is $slogan url is $url\n"; } sub comments_remove () { while (($filecontents[0] = substr ($filecontents[0], 0, index($filecontents[0],"\#"))) =~m/\S/==0) { shift(@filecontents); if (@filecontents==0) {return;} } $filecontents[0]= $filecontents[0]." "; #this is a cludge } # removing the comments and all the empty strings # now $filecontents[0] is non-empty and has all comments removed