Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://166572]
Approved by grep
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2023-03-22 23:20 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (60 votes). Check out past polls.