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

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 )
e.g.
@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
thanks

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

Comment on Pushing multi-dimensional arrays onto each other
Select or Download Code
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' ];
       MeowChow                                   
                   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:

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

    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

    push(@AoA,\@result1,\@result2)

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

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

    HTH



    grep
    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
    push(@a,@b)
    push(@a,[@b])

    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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (17)
As of 2015-07-29 13:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (263 votes), past polls