Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Pushing multi-dimensional arrays onto each other

by cidaris (Friar)
on May 14, 2002 at 20:40 UTC ( #166572=perlquestion: print w/replies, xml ) Need Help??
cidaris has asked for the wisdom of the Perl Monks concerning the following question:

I am returning a multi-dimensional array from MySQL and want to increment it with more results.

( not exact, this is pseudo-output for clarity )
@results1 = 'employee1','address1','payrate1','hiredate1', 'employee2','address2','payrate2','hiredate2' @results2 = 'employee3','address3','payrate3','hiredate3', 'employee4','address4','payrate4','hiredate4'

if I push @results1, @results2, do i get:

'employee1','address1','payrate1','hiredate1', 'employee2','address2','payrate2','hiredate2', 'employee3','address3','payrate3','hiredate3', 'employee4','address4','payrate4','hiredate4'

tried using Dumper to figure this out, to no avail

2002-05-13 Edit by Corion : Fixed CODE tags

Replies are listed 'Best First'.
(MeowChow) Re: Pushing multi-dimensional arrays onto each other
by MeowChow (Vicar) on May 14, 2002 at 21:00 UTC
    That's correct:
    use Data::Dumper; @results1 = ('employee1','address1','payrate1','hiredate1', 'employee2','address2','payrate2','hiredate2'); @results2 = ('employee3','address3','payrate3','hiredate3', 'employee4','address4','payrate4','hiredate4'); push @results1, @results2; print Dumper \@results1; ### OUTPUT ### $VAR1 = [ 'employee1', 'address1', 'payrate1', 'hiredate1', 'employee2', 'address2', 'payrate2', 'hiredate2', 'employee3', 'address3', 'payrate3', 'hiredate3', 'employee4', 'address4', 'payrate4', 'hiredate4' ];
                   s aamecha.s a..a\u$&owag.print
Re: Pushing multi-dimensional arrays onto each other
by grep (Monsignor) on May 14, 2002 at 22:19 UTC
    Maybe I'm mis-interpreting your question, but anyways,
    you ask.

    if I push @results1, @results2, do i get:


    Well if you want a true multi-dimensional array, this isn't it. You just flattened it as MeowChow shows in Data::Dumper.

    To have a multi-dimensional array you want

    | 0 1 2 3 --------------------------------------------------- 0 | ['employee1','address1','payrate1','hiredate1'], 1 | ['employee2','address2','payrate2','hiredate2'], 2 | ['employee3','address3','payrate3','hiredate3'], 3 | ['employee4','address4','payrate4','hiredate4']

    This is Array of Arrays - or more clearly an Array of scalars that contain Array Refs. The '[]' show an array reference.</>

    To achive this you want


    So let's say you want 'address2', you can access it by:

    my $foo = @AoA[1]->[1]


    Unix - where you can throw the manual on the keyboard and get a command
Re: Pushing multi-dimensional arrays onto each other
by csotzing (Sexton) on May 14, 2002 at 21:33 UTC

    The first line will push elements of array b onto array a. The result will be 1-dimensional. The second line pushes @b, as an array, onto @a to create a 2-dimensional array.

    Hope that helps.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://166572]
Approved by grep
[choroba]: that's 2012
[zentara]: I just acquired a computer with the card type, and wondered if it was best to stick with the open-source code or use NVIDIA's proprietary code
[zentara]: do you feel good about a non-open-source code, being in control of your display functions?
[choroba]: If you're not going to play 3d games on it, using the open source driver should be enough
[zentara]: choroba that is true, and the wise choice, but I want to see how fast I can get fglrx3dspheres to run... just as an intellectual challenge :-)

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (10)
As of 2017-05-26 14:00 GMT
Find Nodes?
    Voting Booth?