Makarczyk has asked for the wisdom of the Perl Monks concerning the following question:

Hi- I need to create several arrays of data, however the number of arrays I need to create will be variable based on the file I happen to parse out. The code below fails on the push, but is there any way to accomplish what I am attempting here? thanks for any help you can provide- Matt Makarczyk
$element_count=0; foreach $element (@CLIQUE_ID_START_LINES) { if ($element != $CLIQUE_ID_START_LINES[$#CLIQUE_ID_START_LINES]) { for ($i = $element; $i<=$CLIQUE_ID_START_LINES[$element_count+1] +; $i++) { push(@CLIQUE_DETAILS_$element_count, $VCONFIG[$i]); } $element_count++ }

Replies are listed 'Best First'.
Re: variable array names
by gellyfish (Monsignor) on Oct 15, 2004 at 14:18 UTC

    You probably don't want to be using the symbolic references to be doing this, they are generally considered evil and unnecessary in most applications. Instead you probably want to be using a hash - something like:

    $element_count=0; my %CLIQUE_DETAILS; foreach $element (@CLIQUE_ID_START_LINES) { if ($element != $CLIQUE_ID_START_LINES[$#CLIQUE_ID_START_LINES]) { for ($i = $element; $i<=$CLIQUE_ID_START_LINES[$element_count+1] +; $i++) { if ( not exists $CLIQUE_DETAILS{$element_count} ) { $CLIQUE_DETAILS{$element_count} = []; } push(@{$CLIQUE_DETAILS{$element_count}}, $VCONFIG[$i]); } $element_count++ }
    Of course that is untested as I don't have your data.

    /J\

      If $element_count is an integer, then there's no reason to use a hash instead of an array. So what you really want is an array of arrays, thus.
      $element_count = 0; foreach $element (@CLIQUE_ID_START_LINES) { if ($element != $CLIQUE_ID_START_LINES[-1]) { for my $i ($element..$CLIQUE_ID_START_LINES[$element_count+1]) { push(@{$CLIQUE_DETAILS->[$element_count]}, $VCONFIG[$i]); } $element_count++ }
        identifying the array name as a hash worked perfectly. Thank you both for your extremely helpful replies. Best regards- Matt