Multidimensional Arrays 
Author Message
 Multidimensional Arrays

        I was wondering if anyone could post a script or two that used
multidimensional arrays - or could summarize the syntax of multi-dim arrays.
I have "Programming Perl" but the line,

        "If you refer to an associatve array element as:
                $foo{$a,$b,$c}
        "
doesn't do much for me.

Thanks,
Noel

--
+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=

         Summer Intern                  |         (w) 415.506.0403
   Oracle Server Technologies           |         (h) 415.593.2271
+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=



Sun, 23 Nov 1997 03:00:00 GMT  
 Multidimensional Arrays
It took me a little bit to figure out multi-dimensional arrays, too.  It is
actually much easier than it sounds at first.  Essentially you fool Perl 4
by using commas in the key to an associative array.  Perl, being the clever
guy that it is, joins the comma separated parts of the key with whatever is in
$;  .  By default $; is \034 , so, unless you plan on using that very character
as part of indexes, your indexes get joined together, behind the scenes.  You
use commas, Perl uses \034, and you can have multidimensional arrays of as
many dimensions as you like.  One neat feature of this scheme is that you
only create as many elements as you need - nice for sparse arrays.  Here is
a trivial example:

$matrix{1, 2} = 75;
$matrix{3, 6} = 60;
$matrix{0, 0} = $matrix{1, 2} + $matrix{3, 6};

print "$matrix{0, 0}\n";

By the way, Perl 5 is much better at this kind of thing.  It has true multi-
dimensional arrays.

Hope this helps.
Jim



Mon, 24 Nov 1997 03:00:00 GMT  
 Multidimensional Arrays
:       I was wondering if anyone could post a script or two that used
: multidimensional arrays - or could summarize the syntax of multi-dim arrays.

#!/usr/local/bin/perl

# ----- create %hash and %next

for ( $i=12; $i<14; $i++ ) {
   for ( $j=$i+1; $j<$i+5; $j++ ) {
      $hash{$i,$j} = $i + $j;
      $next{"$i;$j"} = $i - $j;
   }

Quote:
}

# ----- print %hash

$sep = $;;

for ( keys %hash  ) {

   $i = $list[0];
   $j = $list[1];
   $v = $hash{$i,$j};
   print "i j hash(i,j) = $i $j $v\n";

Quote:
}

# ----- print %next

$sep = ';';

for ( keys %next  ) {

   $i = $list[0];
   $j = $list[1];
   $v = $next{"$i;$j"};
   print "i j next(i,j) = $i $j $v\n";

Quote:
}

--
--
jan b schipmolder



Mon, 24 Nov 1997 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. loading a multidimensional array in another multidimensional array

2. Howdoya use array slices with multidimensional arrays?

3. How to convert multidimensional array to single dimension?

4. Finding the number of elements in a multidimensional array

5. Sorting multidimensional arrays

6. Multidimensional arrays

7. Multidimensional arrays in Perl5

8. Multidimensional arrays

9. HELP!! - Passing multidimensional arrays to C

10. multidimensional arrays

11. Multidimensional arrays

12. sort when using multidimensional arrays

 

 
Powered by phpBB® Forum Software