> -----Original Message-----
> From: Michael Kramer [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, December 26, 2002 1:34 PM
> To: [EMAIL PROTECTED]
> Subject: Simple question.
> 
> 
> I need to make an array with multiple levels example:
> I'm new to perl and have been programming in VBscript for 
> about 2 years now
> so the code here is in vb because i know that better.
> 
> dim my_array(1,3)

No "dim" in perl, since arrays are sized dynamically as needed.

If you include "use strict;" at the top of your program (and you should!),
then you need to introduce the array name with "my":

my @my_array;

> my_array(0,0) = "fred"
> my_array(0,1) = "John"
> my_array(0,2) = "mike"
> my_array(0,3) = "dork"
> 
> my_array(1,0) = "mary"
> my_array(1,1) = "sally"
> my_array(1,2) = "stacy"
> my_array(1,3) = "bigger dork"

You can do this line by line, as above. It would be:

$my_array[0][0] = "fred";
$my_array[0][1] = "John";

and so forth. Subscripts in perl go in square brackets. And instead of
commas, you write multiple sets of brackets.

Here's an alternate way to declare and initialize the array all at once:

   my @array = (
      [ 'fred', 'John', 'mike', dork' ],
      [ 'mary', 'sally', 'stacy', 'bigger dork' ],
   );

You're more likely to see this kind of thing in a Perl program.

> 
> response.write my_array(0,3)  'aka. "dork"

print $my_array[0][3];

> 
> It's like an array inside an array.

Yes, that's exactly what it is. Arrays in perl are always one-dimensional.
Perl uses what are called references to simulate multi-dimensional arrays.
In this example, @my_array contains two elements, each of which is a
reference to a separate (anonymous) array containing four elements. When you
write the two subscripts side by side, Perl traces through the references to
find the right element.

Note also that multi-dimensional arrays in Perl are "ragged". This means
that the elements of @my_array can be different. One could hold a reference
to an array with 20 elements, while the other referenced an array with 3
elements. Or the elements could be single values (scalars in Perl), Or
references to arrays, or objects, or whatever. Same with the nested arrays.

>  I figured our arrays but 
> hashes are
> still a little weird.  This might be a perfect problem fixed 
> by hashes.

Hashes are used when you want to retrieve data using a key, rather than by a
position in a list. I can't tell from your example if this is what you're
needing to do.
 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to