Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Assigning an Array to a Multidimensional Array

by keithbas (Initiate)
on Apr 23, 2002 at 16:03 UTC ( [id://161349]=perlquestion: print w/replies, xml ) Need Help??

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

Is it possible to assign an entire 1-D array to a row in a 2-D array without looping and without using references? Example(What I don't want to do):
while (my @kp_src_t_row=$sth->fetchrow_array()) { for ($column=0; $column<@kp_src_t_row; $column++) { $currentarray[$kp_num][$column] = $kp_src_t_row[$column]; } $kp_num++; }
This is what I want to do in theory but don't have a clue if it can be done:
while (my @kp_src_t_row=$sth->fetchrow_array()) { $currentarray[$kp_num]= @kp_src_t_row $kp_num++; }
Such that in the end @currentarray would contain many rows of the data retrieved from the fetch into @kp_src_t_row. Thanks.

Replies are listed 'Best First'.
Re: Assigning an Array to a Multidimensional Array
by premchai21 (Curate) on Apr 23, 2002 at 16:10 UTC

    "Without using references"? By using two-dimensional arrays, you are already using references; two-dimensional arrays are actually arrays of arrays. See perlref, perldsc, perllol.

    What you are looking for, I think, is the anonymous arrayref generator, which is a set of square brackets with a list inside, evaluating to a reference to a new anonymous array, contents being that list. Then you assign the reference to one of the elements of the "topmost" array. So, for example:

    $two[4] = [ @one ];

    will cause element 4 of @two (the two-dimensional array) to contain a reference to an anonymous array initialized from @one (a one-dimensional array), thus assigning @one to row 4 (counting from 0, assuming the indexing goes row, column). Again, see perlref, perldsc, perllol.

Re: Assigning an Array to a Multidimensional Array
by perlplexer (Hermit) on Apr 23, 2002 at 16:07 UTC
    Take a look at perldoc perldsc
    What you need can be done in the following way:
    while (my @kp_src_t_row = $sth->fetchrow_array()) { $currentarray[$kp_num]= [ @kp_src_t_row ]; $kp_num++; }
    --perlplexer
Re: Assigning an Array to a Multidimensional Array
by gryphon (Abbot) on Apr 23, 2002 at 16:14 UTC

    Greetings keithbas,

    Why exactly do you not want to use references? References are your friend. If I understand complex data structures (which maybe I don't), I'd think that any array of an array is built only by references.

    Alternatively, you could push all the array data into a single, flat array. Assuming that the number of columns will never change, you could then just loop through the array...

    push @currentarray, @new_data while (my @new_data = $sth->fetchrow_array);

    This is really icky, IMHO, but you'd have your data in an array with no references...

    -gryphon
    code('Perl') || die;

Re: Assigning an Array to a Multidimensional Array
by gav^ (Curate) on Apr 23, 2002 at 17:13 UTC
    What about: my $currentarray = $sth->fetchall_arrayref; I don't think @currentarray is the greatest of variables names...

    gav^

Re: Assigning an Array to a Multidimensional Array
by drewbie (Chaplain) on Apr 23, 2002 at 20:44 UTC
    Is it possible to assign an entire 1-D array to a row in a 2-D array without looping and without using references?

    If you're new to references, and that is why you are wanting to not use them, I strongly suggest you sit down and get your head wrapped around them. You will not be able to implement complex data structures without them. And life will be much easier with them anyway.

    If you don't understand references, I hope you don't feel dumb. Because it took me a long time for the concepts to settle in my brain. But one day I was talking with someone about it and it just clicked. From then on, it's been smooth sailing. Whatever time it takes you to understand references will be time well spent. In the long run you will benefit immensely.

    And to answer your question, in many instances, you can't achieve what you're wanting anyway (the multi-dimentional part) without using references. To assign an array as an array element requires references - there is no other way.

    Good luck & don't give up.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-03-29 13:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found