SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist FCC's Avatar
    Join Date
    May 2006
    Posts
    417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Making many arrays

    Hi all,

    I want to basically make a multidimensional array. My problem is I don't want to have to explicitly declare 20 arrays and then my other other reference all 20 of them. But rather have 2 foreach loops:

    Code:
    foreach my $i (0 .. 6){
      foreach my $j ( 0 .. 30){
        $snps[$i] = \new @array()
      }
    }
    Where new @array() just makes an array without any explicit name. Obviously the above code doesn't work. I hope I make sense...is there anyway to do this?

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Is this perl?

  3. #3
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Makes an array of references to 20 empty arrays

    Code:
    my @array = ();
    for (1 .. 20) {#<-number of arrays
       push @array,[];
    }
    You don't have to set the number of buckets that each array will have when you create them unless there is a real need to do that. Its also pretty rare that you would need to create the array references first. Perl will just make them as necessary during the runtime of your program.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •