CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Making a high scores table

 



psynt555
New User

Dec 1, 2012, 7:10 AM

Post #1 of 2 (1438 views)
Making a high scores table Can't Post

Basically, I have a set of scores that will be pushed into an array each time the program loops (i.e. the person says they want to play again)

This is my code so far:

<code>

print "Your final score is: $score_total\n\n"; # score organiser.
print "Please enter your initials for the high score table: \n";
chomp (my $initial_add = <STDIN>);
my @initials_array = ();
push (@initials_array, $initial_add);
my @score_array = ($score_total);
push (@score_array, $score_total);
print "Name = $initials_array[0]\tScore = $score_array[0]\n";

print "Initials \t Score \n";

</code>

It is worth noting that the variable $score_total is the score from an earlier bit of code, every time this bit finishes and the $score_total is pushed into @score_array, the $score_total gets intialized to 0 again so a new score can be added.

How can I go about putting this in a high scores table that is displayed as the above code part : print "Name = $initials_array[0]\tScore = $score_array[0]\n";
I want it to add the scores each time the game is played so that the high scores table gets more populated.

THANKS IF ANYONE CAN HELP!!


BillKSmith
Veteran

Dec 1, 2012, 8:53 AM

Post #2 of 2 (1431 views)
Re: [psynt555] Making a high scores table [In reply to] Can't Post

Note: Use square brackets rather than angle brackets around your code tags in this forum.

The variables @initials_array and @score_array must be declared outside the loop. (They are reinitialized every time your code is run.)

The function "push" adds to the end of the list. Your subscript [0] will always access the first element. Use [-1] to access the last element.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives