Code Comments
Programming Forum and web based access to our favorite programming groups.I've been thinking about this and not sure how to approach this problem. Say I want to create an array of 4 array references, thats easy since I know that there will be 4 array references, how would I do this dynamically? Say if one I only needed 2 references and another I need 10 array references, is there a way to do this in perl? Thanks in advance.
Post Follow-up to this messageScott Pham wrote:
> I've been thinking about this and not sure how to approach this
> problem. Say I want to create an array of 4 array references, thats
> easy since I know that there will be 4 array references, how would I
> do this dynamically? Say if one I only needed 2 references and
> another I need 10 array references, is there a way to do this in
> perl? =20
#!/usr/bin/perl -w
use warnings;
use strict;
my @array;
my $num_child_arrays =3D 10;
for (my $count =3D 0; $count < $num_child_arrays; $count++) {
my $array_ref =3D [ ];
push (@array,$array_ref);
}
Could probably be done in a shorter format as well...
push (@array,[]) for (1..$num_child_arrays);
For more info:
perldoc -f push
Post Follow-up to this messagepush @array, [] while @array < 10; also works. "Ed Christian" <edc@corp.ptd.net> wrote... push (@array,[]) for (1..$num_child_arrays); For more info: perldoc -f push
Post Follow-up to this messageScott Pham wrote: > I've been thinking about this and not sure how to approach this > problem. Say I want to create an array of 4 array references, thats > easy since I know that there will be 4 array references, how would I > do this dynamically? Say if one I only needed 2 references and > another I need 10 array references, is there a way to do this in perl? It is not necessary to predeclare arrays in Perl. They are by nature dynamic and you can expand and shrink them as needed. What is the specific problem you're having?
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.