Jon Serra wrote:

>Greetings,
>
>        I have an array, each element will contain a reference to another array. How 
>can I dynamically generate each of those references such that each reference is 
>unique. I am trying to create dynamic 2d arrays.  TIA JON
>
#
# The key is understanding 'my' and how perl's garbage collection works.
#
# Each time we enter a sub, my creates new variables and when we return
# a reference to those variables they are ours to keep (unlike C).
#
# The next time we reach the sub we will get new variables and the old
# will be unaffected.
#
my @array = (['a1','a2','a3'],['b1','b2','b3'],['c1','c2','c3']);
print_array(@array);

my @other_array = make_2d_array;
print_array(@other_array);

sub make_2d_array {
    my @array;
    # Sorry for using ++ on a string!  I couldn't resist.
    # $i = 'a';
    # ++$i;
    # now $i eq 'b'!!!
    push(@array,make_simple_array($i)) for(my $i='a';$i le 'c';++$i);
}

sub make_simple_array {
    my $rowlabel = shift;
    my @array = ();
    push(@array,$rowlabel.$i) for(my $i=1;$i<4;++$i);
    return \@array;
}

sub print_array {
    for my $arrayref (@_){
        for my $element (@$arrayref){
            print "$element\t";
        }
        print "\n";
    }   
}




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to