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:
2D Array with random numbers

 



Lynn
Novice

Sep 17, 2012, 1:02 PM

Post #1 of 5 (2234 views)
2D Array with random numbers Can't Post

I am very lost on this one. I need to make a 2D array with 5 random numbers added to it. I can't find online, any thing that will help. Can anyone guide me in the right direction like a good tutorial that will explain this.
Thanks


Chris Charley
User

Sep 17, 2012, 6:54 PM

Post #2 of 5 (2228 views)
Re: [Lynn] 2D Array with random numbers [In reply to] Can't Post

Two places in the documents that will explain 2-dimensional arrays, their use, how they're created and accessed.
http://perldoc.perl.org/perllol.html and http://perldoc.perl.org/perldsc.html


Laurent_R
Veteran / Moderator

Sep 17, 2012, 8:16 PM

Post #3 of 5 (2225 views)
Re: [Lynn] 2D Array with random numbers [In reply to] Can't Post

Try something like this to populate a 2D array:


Code
my @two_D_array; 

foreach my $i (0..4) {
foreach my $j (0..4) {
$two_D_array[$i][$j] = 1;
}
}


There could be a number of shortcuts which could reduce the nember of lines in the code, but I wanted to make it as simple as possible for you to understand the principle.

Of course, here I have just put ones in each element of the 2D_array. You would have to put instead your random numbers in the appropriate range required by what you want to do.


Lynn
Novice

Sep 17, 2012, 8:53 PM

Post #4 of 5 (2224 views)
Re: [Laurent_R] 2D Array with random numbers [In reply to] Can't Post

Thank you! That helps greatly!


Laurent_R
Veteran / Moderator

Sep 18, 2012, 5:25 AM

Post #5 of 5 (2214 views)
Re: [Lynn] 2D Array with random numbers [In reply to] Can't Post


In Reply To
Thank you! That helps greatly!


You should still read the documents pointed by Chris, there is much much more to it than the single code I have shown you.

 
 


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

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