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: Beginner:
"uninitialized value" when I'm certain I initialized it

 



Yinyang107
New User

Dec 6, 2011, 8:01 AM

Post #1 of 2 (586 views)
"uninitialized value" when I'm certain I initialized it Can't Post

As my source file's name implies, this is for a college assignment. It's eventually going to draw five cards and give them a score according to poker rules: pair, full house, et al. The relevant bit of code is this:

Code
my (@ranks) = sort($card1{rank}, $card2{rank}, $card3{rank}, $card4{rank}, $card5{rank}); 
my (%count);
for(my $i = 2; $i <=14; $i++){
$count{$i} = 0;
print "assigned $count{$i} to $i\n";
}
foreach my $item (@ranks) {
print "$item $count{$item} ";
$count{$item}++;
print "$count{$item}\n";
}
%count = sort{ $b <=> $a } %count;
print "\n$card1{rank}\n";
print "$card2{rank}\n";
print "$card3{rank}\n";
print "$card4{rank}\n";
print "$card5{rank}\n\n";
#while( (my $k, my $v) = each %count){
# print "$k => $v\n";
#}
for(my $i = 2; $i <=14; $i++){
print "$i => $count{$i}\n";
}


$card1{rank} and the others start with a random integer from 2-14 inclusive. When I run the program, I get this:

Quote
assigned 0 to 2
assigned 0 to 3
assigned 0 to 4
assigned 0 to 5
assigned 0 to 6
assigned 0 to 7
assigned 0 to 8
assigned 0 to 9
assigned 0 to 10
assigned 0 to 11
assigned 0 to 12
assigned 0 to 13
assigned 0 to 14
10 0 1
10 1 2
5 0 1
6 0 1
8 0 1

10
8
5
10
6

Use of uninitialized value in concatenation (.) or string at assign1.pl line 60.
2 =>
3 => 2
Use of uninitialized value in concatenation (.) or string at assign1.pl line 60.
4 =>
5 => 4
Use of uninitialized value in concatenation (.) or string at assign1.pl line 60.
6 =>
7 => 6
Use of uninitialized value in concatenation (.) or string at assign1.pl line 60.
8 =>
9 => 8
Use of uninitialized value in concatenation (.) or string at assign1.pl line 60.
10 =>
11 => 10
Use of uninitialized value in concatenation (.) or string at assign1.pl line 60.
12 =>
13 => 12
Use of uninitialized value in concatenation (.) or string at assign1.pl line 60.
14 =>

The first chunk of output tells me that there's values there, but as you can see, later on they're uninitialized. Any tips?

Full code is attached.


(This post was edited by Yinyang107 on Dec 6, 2011, 8:03 AM)
Attachments: assign1.pl (1.85 KB)


BillKSmith
Veteran

Dec 6, 2011, 12:27 PM

Post #2 of 2 (570 views)
Re: [Yinyang107] "uninitialized value" when I'm certain I initialized it [In reply to] Can't Post

Your immediate problem is the sort on line 51. I am not sure what you intend it to do. As written, it does not make any sense. (The hash is converted into an array consting of alternating keys and values. That array is sorted into numerical order. The resulting array is then assigned back to the hash using every other array element as a hash key.)

Your program runs without errors when I remove that statement.
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