|
|
 |
|
Title: Multi Dimesional Array Program Code in Perl By Rohit D'souza
Submitter: rohit d'souza
(other recipes)
Last Updated: 2006/05/05
Version no: 1.0
Category:
Miscellaneous
|
|
|
Description:
it helps u learn how to create multidimesional arrays in perl using array references and then how to print the values of that array
Usage: Text Source
print "\n";
@arr1=(1,2,3,4);
@arr2=(5,6,7,8);
$array1=\@arr1;
$array2=\@arr2;
@multi=($array1,$array2);
foreach $val (@multi)
{
for($i=0;$i<=$#arr1;$i++)
{
print "$val->[$i]\t";
}
print "\n";
}
Output:
1234
5678
The license for this recipe is available here.
Discussion:
|
|
Add comment
|
|
Number of comments: 1
Printing Multidimensional Arrays, Mark Manning, 2008/04/12
Here is a good question: How can you use the MAP command to print out a multidimensional array in an orderly fashion?
Then the second question is: How can you read that same array back in using MAP in an orderly fashion?
For instance:
my @array = ();
my $i = 0;
my $j = 0;
my $k = 0;
for( $i=0; $i<100; $i++ ){
for( $j=0; $j<100; $j++ ){
for( $k=0; $k<10; $k++ ){
$a[$i][$j][$k] = $i+$j+$k;
}
}
}
open( THEFILE, ">out.dat" ) || die $!;
print THEFILE map {pack("C",$_)} $a[0..100][0..100][0..10];
close( THEFILE );
exit( 0 );
The above does not work. I would have thought that it would do the entire array. Instead, I get a single NULL character. Any ideas why?
Also, if you use the "@a" instead of the "$a" - you get errors.
Add comment
|
|
|
|
|
 |
|