ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

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

 

Not Rated yet


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

#/usr/bin/perl

    print "\n";
    #defining two arrays

    @arr1=(1,2,3,4);
    @arr2=(5,6,7,8);
    #assigning refrences of those two arrays to two variables

    $array1=\@arr1;
    $array2=\@arr2;
    #defining a multi dimesional array

    @multi=($array1,$array2);
    #printing values from the array

    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



Highest rated recipes:

1. Breaking down a URI into ...

2. Finding Palindromes

3. Extracting HTML URL Links

4. Removing dangerous ...

5. Matching Royal Mail ...

6. Finding URLs in text -- ...

7. Validating email ...

8. Validate Domain Names

9. Extract the Korean ...

10. Remove any HTML




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.