Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array counter question

Status
Not open for further replies.

amishjj

Programmer
Feb 23, 2005
2
US
Hi, any help here would be greatly appreciated. I'm new to programming and am unable to test anything while I'm at work.

Basically, I'm sending 400 people to a questionairre but I need each sequential person to get a different html page from a predefined array (1st user to A1.html, 2nd to A2.html etc.) until all elements in the array are exhausted, then reset the counter to 0. I've got the following code, but I'm afraid that since $n=0 is always executed, it will send all users to A1.html.

#!/usr/bin/perl
print "content-type: text/html \n\n";
print "<a href=";

#array of pages
@pages = qw(A1.html A2.html A3.html A4.html B1.html B2.html B3.html B4.html C1.html C2.html C3.html C4.html D1.html D2.html D3.html D4.html);

#element index counter
$n = 0;

#page delivered is sequential element in array
$deliver_page = @pages[$n]

#one iteration through the array
if ($n <= 15 {
print "$deliver_page>Click Here</a>";
$n++;
}

#end of array, start again
else {
$n = 0;
print "$deliver_page>Click Here</a>";
$n++;
}
}

Does anyone know if this will work? Or do I need to store and increase a number in a .txt file each time the script is called? I really appreciate any feedback. Thanks.
 
If you mean each time a different person takes the survey you want each person to start at the next page in the sequence, then yes, store the page info in a file and open the file to determine what page should be accessed next. You could store the info as the index number of the array holding the name of the page or the actual name of the page, whatever works best for you.
 
Hello amishjj,

I am not a pro at cgi by any stretch of the imagination. However, I did stay at a Holiday Inn Express last night. LOL. Kidding of course.

Anyway, I had a need for something very similar to what you are trying to accomplish. I have tweeked my script a bit, but it works nonetheless.

###################################
###################################
#!/usr/bin/perl

$logpath = "numLog.txt";
open (LOG, "$logpath");
@xNum = <LOG>;
close(LOG);
$pageNum = $xNum[0];
$pageNum++;
$newPageNum = $pageNum;

open (LOG, ">$logpath");
flock(LOG, 2);
if ($newPageNum == 16) {
print LOG "0"; }
else { print LOG "$newPageNum"; }
flock(LOG, 8);
close(LOG);

if ($newPageNum == 1) { $sendTo = "A1.html"; }
if ($newPageNum == 2) { $sendTo = "A2.html"; }
if ($newPageNum == 3) { $sendTo = "A3.html"; }
if ($newPageNum == 4) { $sendTo = "A4.html"; }
if ($newPageNum == 5) { $sendTo = "B1.html"; }
if ($newPageNum == 6) { $sendTo = "B2.html"; }
if ($newPageNum == 7) { $sendTo = "B3.html"; }
if ($newPageNum == 8) { $sendTo = "B4.html"; }
if ($newPageNum == 9) { $sendTo = "C1.html"; }
if ($newPageNum == 10) { $sendTo = "C2.html"; }
if ($newPageNum == 11) { $sendTo = "C3.html"; }
if ($newPageNum == 12) { $sendTo = "C4.html"; }
if ($newPageNum == 13) { $sendTo = "D1.html"; }
if ($newPageNum == 14) { $sendTo = "D2.html"; }
if ($newPageNum == 15) { $sendTo = "D3.html"; }
if ($newPageNum == 16) { $sendTo = "D4.html"; }


print "content-type: text/html \n\n";
print "<a href=";
print "\"$sendTo\">CLICK HERE TO GO TO PAGE</a>";

#############################
#############################


Now, I am sure there are some gurus out there that will eat this one alive. But as I said, it works (atleast for me). You would, of course, need to change it to meet your needs as well.

If it helps, great!

If not, I tried.

Later!
IamStang
 
Hi IamStang, that should work OK, but in case you ever need to do something like this again, you might consider something along these lines:

Code:
#!perl

use strict;
print "content-type: text/html \n\n";

my $logpath = "numLog.txt";
open (LOG, "$logpath") or die "$!";
my $pageNum = <LOG> || 1;
close(LOG) or die "$!";

my %newPageNum = (
   1 => 'A1.html',
   2 => 'A2.html',
   3 => 'A3.html',
   4 => 'A4.html',
   5 => 'B1.html',
   6 => 'B2.html',
   7 => 'B3.html',
   8 => 'B4.html',
   9 => 'C1.html',
  10 => 'C2.html',
  11 => 'C3.html',
  12 => 'C4.html',
  13 => 'D1.html',
  14 => 'D2.html',
  15 => 'D3.html',
  16 => 'D4.html'
);

print "<a href=\"$newPageNum{$pageNum}\">CLICK HERE TO GO TO PAGE</a>";

$pageNum++;
$pageNum = 1 if $pageNum > 16;

open (LOG, ">$logpath") or die "$!";
flock(LOG, 2);
print LOG "$pageNum";
flock(LOG, 8);
close(LOG) or die "$!";
 
Thanks a ton you guys, you saved me a ton of grief. The snipets of code are great. Much appreciated!
 
Hello KevinADC,

As I am new to cgi, what makes your script better than mine? I dont mean that in a defensive tone. I am just trying to learn a few things here and there.

I can see that yours is using strict. But I, as of yet, have come upon an explanation as to why "strict" is better. It seems to me that by not using the "strict", the snippet would be easier to incorporate into an existing script.
 
Using a hash (to look up which page to put in the link based on the number in the log file) is a method that can be beneficial in many situations. It dispenses with the need to use all those if (condition) {action} blocks in your code, which would be better coded as:

if (){
...
}
elsif () {
...
}
elsif () {
...}
etc
etc
etc
else {...}

you should use if/elsif/else when only one of the conditions needs to be true, because perl will stop checking once a true condition is found. Using all if's like you did forces perl to check each condition even if the very first one is true. In a short script like yours this is no big deal and was not meant to be critical of your code. :)
 
I dont mind people being critical of my scripts. It is but only one of the many ways to learn. Therefor, I welcome it. Unlike a great deal of others out there.

I see your point in your last post and will definately keep that in mind for any scripts I write in the future. Thanks for the input.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top