For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2004 > array of references









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author array of references
Scott Pham

2004-10-25, 3:56 pm

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.

Ed Christian

2004-10-25, 8:55 pm

Scott 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

Zeus Odin

2004-10-25, 8:55 pm

push @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


Bob Showalter

2004-10-26, 3:56 pm

Scott 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?
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com