Create and fill a two-dimensional array

An example which shows how to create a two-dimensional array in Perl, fill it with data, pass it around via return() and print the array output.

print $twodimensionalarray[0][0];

should not work anymore. You should use the syntax suggested below.

 

sub getsubdomains()
{
  #Create an empty, two-dimensional array
  my @subdomains = ( [], [] );
  #Populate it, in this case from a MySQL SELECT query
  while (my @sql_rows=$syssth->fetchrow_array)
  {
  ${@subdomains}[$i][0]= $sql_rows[0];
  ${@subdomains}[$i][1]= $sql_rows[1];
  $i++;
  }
 return @subdomains;
 #Pass it to the function which needs it
}

sub new_postform()
{
 #Retrieve our old array and save it
 my @subdomains=getsubdomains(); 
 my $html_subdomainselect='<select name="p_subdomain">';
 #Populate an HTML <SELECT> Box with the data
 for(my $i=0;$i<=$#subdomains;$i++)
 {
 $html_subdomainselect .= '<option value="'.${@subdomains}[$i][0].'">'.${@subdomains}[$i][1].'</option>';
 }
 $html_subdomainselect .= '</select>';
}

Source: (2007-01-23 21:36:56)