Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Arrays that points to another array

by TASdvlper (Monk)
on Jan 29, 2004 at 20:59 UTC ( [id://325037]=perlquestion: print w/replies, xml ) Need Help??

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

All,

I would like to set up an array that points to another array (basically, each index points to another anonymous array). But, I want to build the second array within a for loop. For example:

my @myarray for (1..5) { # here I would like $myarray[0] = ( 1, 2, 3, 4, 5 ); } for (6..10) { # here I would like $myarray[1] = ( 6, 7, 8, 9, 10 ); } ## and so on.
I tried using push($myarray[0], $_) within the for loops, but that didn't work.

Any thoughts on how to do this ?

Thanks in advance.

Replies are listed 'Best First'.
Re: Arrays that points to another array
by hardburn (Abbot) on Jan 29, 2004 at 21:09 UTC

    First, read over perlreftut and perlref, as Perl's complex data structures are all based on references. Next, perldsc gives you the complete overview on making Arrays of Arrays, as well as other kinds of Perl data strucutres (like Arrays of Hashes).

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    : () { :|:& };:

    Note: All code is untested, unless otherwise stated

      hardburn is right. Sound advice.

      I would only add perllol, which seem particularly suited to your blem...

      dave

Re: Arrays that points to another array
by Roy Johnson (Monsignor) on Jan 29, 2004 at 21:11 UTC
    perldoc perlreftut in general, and for your specific question:
    push(@{$myarray[0]}, $_);

    The PerlMonk tr/// Advocate
Re: Arrays that points to another array
by Roger (Parson) on Jan 29, 2004 at 23:48 UTC
    I tried using push($myarray[0], $_) within the for loops, but that didn't work.

    You almost got it right... should be push @{$myarray[0]}, $_;

    I see NetWallah has posted a good solution, here are my alternatives...
    use strict; use warnings; use Data::Dumper; my @array = (); for my $i (0..1) { for ($i*5+1..$i*5+5) { push @{$array[$i]}, $_; # push an element at a time } } print Dumper(\@array); @array = (); # one-liner with map and slice. @array[0..1] = map { [ $_*5+1 .. $_*5+5 ] } 0..1; print Dumper(\@array);
Re: Arrays that points to another array
by NetWallah (Canon) on Jan 29, 2004 at 22:35 UTC
    Perhaps something like this ..
    my @a; for(0..3){ $a[$_]=[$_*5+1..$_*5+5]; print qq(a[$_]= @{$a[$_]}\n); } --Output-- a[0]= 1 2 3 4 5 a[1]= 6 7 8 9 10 a[2]= 11 12 13 14 15 a[3]= 16 17 18 19 20
    "When you are faced with a dilemma, might as well make dilemmanade. "
Re: Arrays that points to another array
by allolex (Curate) on Jan 29, 2004 at 22:28 UTC

    In addition to the excellent advice from Roy_Johnson and hardburn, you might find it easier to understand the references* involved by having a look at a tutorial on this site called intro to references. It has cool ASCII-art diagrams of what the data structures look like.

    * An AoA, or Array of Arrays is actually an array of references pointing to arrays.

    --
    Allolex

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://325037]
Approved by bart
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-19 04:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found