In article <[EMAIL PROTECTED]>, [EMAIL PROTECTED] (Evil Bag Piper) wrote:
> Hello List, > > Long time reader, first time poster. I have created a script that will > generate 10 random numbers between 1 and 50. The numbers are assigned > to variables in order from one to ten. The variables are $random_one_a, > $random_two_a, $random_three_a and so on all the way to $random_ten_a. > Here's the script for that: > > $random_one_a = int(rand(49))+1; > $random_two_a = int(rand(49))+1; > $random_three_a = int(rand(49))+1; > $random_four_a = int(rand(49))+1; > $random_five_a = int(rand(49))+1; > $random_six_a = int(rand(49))+1; > $random_seven_a = int(rand(49))+1; > $random_eight_a = int(rand(49))+1; > $random_nine_a = int(rand(49))+1; > $random_ten_a = int(rand(49))+1; > > So the computer might generate the following numbers: > 30, 10, 5, 6, 20, 49, 50, 2, 4, 12 > > That means that: > $random_one_a = 30 > $random_two_a = 10 > $random_three_a = 5 > so on and so on.... > > I then assign all the variables to an array: > > @numbers_a = ("$random_one_a", "$random_two_a", "$random_three_a", > "$random_four_a", "$random_five_a", "$random_six_a", "$random_seven_a", > "$random_eight_a", "$random_nine_a", "$random_ten_a"); > > Then I sort them to get them in ascending order: > > @sorted_a = sort{$a<=>$b}(@numbers_a); > > I can then print out the @sorted_a array and the numbers appear on the > screen in order from lowest to highest. The problem comes next. I want > to take those ten numbers in ascending order and assign them to > variables again (from lowest to highest. Here's what my script looks > like: > > foreach $anumber (@sorted_a) { > @sorted_field = split (/,/, $anumber); > $randomonea = $sorted_field[0]; > $randomtwoa = $sorted_field[1]; > $randomthreea = $sorted_field[2]; > $randomfoura = $sorted_field[3]; > $randomfivea = $sorted_field[4]; > $randomsixa = $sorted_field[5]; > $randomsevena = $sorted_field[6]; > $randomeighta = $sorted_field[7]; > $randomninea = $sorted_field[8]; > $randomtena = $sorted_field[9]; > } > > This should assign $randomonea to the number 2, $randomtwoa to the > number 4, $randomthreea to the number 5 so on and so on. The only > number that shows up when I print these newly assigned variables is the > highest number. For this example, if I printed out all of these newly > assigned variables the only thing that would appear would be: > > $randomonea = 49 > > The rest of the variables show up as nothing. Not even 0's. > > My questions are: > 1) Why does it take the first newly assigned variable $randomonea (which > should be the lowest number) and give it a value of the highest number > in the array? > > 2) Why don't the other nine values in the array show up as being > assigned to the new variables? > > 3) Is this problem in my sorting or is it the actual assigning of the > array members? > > If you would like to see the entire code please email me. I don't want > to include it for obvious email cosmetic reasons. > > Thank you all in advance, > Roger Spears oy, lots of work #!perl use warnings; use strict; my @sortedrands = sort{$a<=>$b} map{ int(rand(49))+1 } 1..10; print join( "\t", @sortedrands), "\n"; ######### I don't understand why you do this: > foreach $anumber (@sorted_a) { > @sorted_field = split (/,/, $anumber); when the @sorted_a's list items are not themselves separated by commas, but individually in the array elements themselves. however, would it be so hard to change instances of $randomninea in your code to something more sensible like $sortedrands[9] instead? :) This way it's not necessary to re-assign to another variable, which is why you'd want a list in the first place, but perhaps I misunderstand the end-result you're looking for. :-) -- Scott R. Godin | e-mail : [EMAIL PROTECTED] Laughing Dragon Services | web : http://www.webdragon.net/ It is not necessary to cc: me via e-mail unless you mean to speak off-group. I read these via nntp.perl.org, so as to get the stuff OUT of my mailbox. :-) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]