Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Mar 2010
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation array and counting distinctions

    Hello everyone,

    I am a CGI student completely new to the subject. I have a project due in two days, and I've been struggling with it all week. Unfortunately, my instructor poorly teaches the subject, and I am out of resources but the online world. May you please help me?

    I have an html page that contains a form. This form is simply a list of radio buttons of college names.

    In CGI, I must return the college name to the user..in addition to the college mascot (which is not a value in the html form). I'm lost about how to set up this array?

    Lastly, I must also return a counter that is counting how many times the user clicks on a college radio button. I don't know where to start. The array scenario is giving me the most difficult time. Do I use an index array or hash? How do I communicate the college name with the mascot together?

    I would appreciate any help you may have to offer. Thank you for your time in reading.

  • #2
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    where are the details of the mascot etc stored?

    If you need to count how many times a user clicks on a radio button, you will likely need to use javascript.

    welcome; by the way.

    bazz
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #3
    New Coder
    Join Date
    Mar 2010
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks bazz,

    I am attempting to store details of the mascot in a hash array. Considering that my radio button form only has the value of college. My hash array will store the college next to its mascot.

    My CGI code currently looks something like this:
    I would appreciate any advice or feedback.

    Code:
    #!/usr/bin/perl
    #final.cgi - saves form data to a file, and creates a dynamic
    #Web page that displays a message and survey statistics
    print "Content-type: text/html\n\n";
    use CGI qw(:standard -debug);
    use strict;
    
    #declare variables
    my ($college, $mascot, $size, @records, @errors);
    my %college_count = ("Baker", 0,
    				  "Michigan", 0,
    				  "State", 0,
    				  "Eastern", 0,
    				  "Central", 0,
    				  "Wayne", 0,
    				  "Lawrence", 0,
    				  "Mercy", 0);
    my %mascot = ("Baker", "Bears", 
    			  "Michigan","Wolverines", 
    			  "State", "Spartans", 
    			  "Eastern", "Eagles", 
    			  "Central", "Chippewas", 
    			  "Wayne", "Warriors", 
    			  "Lawrence", "Devils", 
    			  "Mercy", "Titans"); 
    
    #assign input items to variables
    $college = param('College');
    
    #validate input data
    if($college ne "Baker" and $college ne "Michigan" and $college ne "State" and $college ne "Eastern" and $college ne "Central" and $college ne "Wayne" and $college ne "Lawrence" and $college ne "Mercy"){
    	push(@errors, "Select a college");
    }
    
    #determine size of @errors array
    $size = @errors;
    
    #process input data or display error page
    if($size == 0){
    	#process input data
    	#save form data to a file
    	open(OUTFILE, ">>", "survey.txt") 
    		or die "Error opening survey.txt. #!, stopped";
    	print OUTFILE "$college\n";
    	close(OUTFILE);
    
    	#calculate survey statistics
    	open(INFILE, "<", "survey.txt") 
    		or die "Error opening survey.txt. $!, stopped";
    	@records = <INFILE>;
    	close(INFILE);
    	foreach my $rec (@records) {
    		chomp($rec);
    		($college) = split(/,/, $rec);
    		$college_count{$college} = $college_count{$college} + 1;
    	}
    
    	#generate HTML acknowledgment
    	print "<HTML><HEAD><TITLE>Name that Mascot</TITLE></HEAD>\n";
    	print "<BODY>\n";
    	print "<H2>Thank you for visiting the American College web site!</H2>\n";
    	
    	print "<H2>You selected $college.</H2>\n";
    	
    	foreach my $key ("Baker", "Michigan", "State", "Eastern", "Central", "Wayne", "Lawrence", "Mercy"){
       		print "$key is home of the $mascot{$key}\n";
    	} 
    
    	print "<EM><B>See the current college results below:</EM></B>\n";
    	print "<TABLE>\n";
    	foreach my $key ("Baker", "Michigan", "State", "Eastern", "Central", "Wayne", "Lawrence", "Mercy") {
    		print "<TR><TD>$key</TD>  <TD>$college_count{$key}</TD></TR>\n";
    	}
    	print "</TABLE>\n";
    	print "</BODY></HTML>\n";
    }
    else {
    	#display error page
    	print "<HTML><HEAD><TITLE>Name that Mascot</TITLE></HEAD>\n";
    	print "<BODY>\n";
    	print "<H2>You did not make a valid selection. Please click the BACK button and your browser \n";
    	print "and complete the survey. Thank you! </H2><BR>\n";
    	for(my $x = 0; $x < $size; $x = $x +1){
    		print "$errors[$x]<BR>\n";
    	}
    	print "</BODY></HTML>\n";
    }

  • #4
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    OK, this is a homework and whilst you are making a good effort (in my opinion), the help we can give is limited. That said; the help I can give anyway is limited but I can give a few tips.

    1. when a web page is being viewed and the user checks radio buttons, the page must then be submitted as a form for a second (processing) script to evaluate what was entered to that form. then you can see what college they chose and what the relveant mascot is.

    If I understand your requirement that you need to know how many times the user clicked a raido button, eg he clicked button 1 three times, you'll need the client side scripting to work that out - javascript.

    Because you have already worked out how to use arrays, my tip would be that the hash %college_count may be better as an @array which you can grep through instead of having this line..
    Code:
    if($college ne "Baker" and $college ne "Michigan" and $college ne "State" and $college ne "Eastern" and $college ne "Central" and $college ne "Wayne" and $college ne "Lawrence" and $college ne "Mercy"){
    	push(@errors, "Select a college");
    }
    you could have this (pseudo code)

    Code:
    unless ( grepping here ) {
      push( @errors, 'message' );
    }
    Thne you can use that @array in your foreach loop

    Code:
    foreach my $var (@array){
    process the output here [erhaps with a condition to check a match of the $var in the $hash{$var};
    }
    Besides that, I would suggest you read up on erm, something. can't recall the best title but basically, unless the value is a $variable, it should go in single quotes and if it is a variable, it should not have double quotes or nay quotes for that matter.

    hth

    bazz
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #5
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,836
    Thanks
    2
    Thanked 158 Times in 153 Posts
    You're using the wrong data structure. Instead of 2 separate hashes, you should use 1 HoH (hash-of-hashes).

    Code:
    my %college = (
                  Michigan => {
                              count => 0,
                              mascot => "Wolverines",
                              },
                  
    );

  • #6
    New Coder
    Join Date
    Mar 2010
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you all so much for your sharing of knowledge and help. After much debugging and more research, I was able to get my CGI working. I really appreciate all the help you have given to me. I learned so much, thank you.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •