Data and Data Structures

List of Lists

Declaration
Generation
Access and Printing
Example

Declaration
  @LoL = (
         [ "fred", "barney" ],
         [ "george", "jane", "elroy" ],
         [ "homer", "marge", "bart" ],
       );

Generation
  # reading from file
  while ( <> ) {
      push @LoL, [ split ];
  }

  # calling a function
  for $i ( 1 .. 10 ) {
      $LoL[$i] = [ somefunc($i) ];
  }

  # using temp vars
  for $i ( 1 .. 10 ) {
      @tmp = somefunc($i);
      $LoL[$i] = [ @tmp ];
  }

  # add to an existing row
  push @{ $LoL[0] }, "wilma", "betty";

Access and Printing
  # one element
  $LoL[0][0] = "Fred";

  # another element
  $LoL[1][1] =~ s/(\w)/\u$1/;

  # print the whole thing with refs
  for $aref ( @LoL ) {
      print "\t [ @$aref ],\n";
  }

  # print the whole thing with indices
  for $i ( 0 .. $#LoL ) {
      print "\t [ @{$LoL[$i]} ],\n";
  }

  # print the whole thing one at a time
  for $i ( 0 .. $#LoL ) {
      for $j ( 0 .. $#{ $LoL[$i] } ) {
          print "elt $i $j is $LoL[$i][$j]\n";
      }
  }

Table of Contents.
Next.