CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:
passing multidim. arrays out of functions



Dec 29, 2000, 7:35 PM

Post #1 of 2 (270 views)
passing multidim. arrays out of functions Can't Post

I have created a small script that reads a programmer specified flat file and creates 2 multi-dimensional arrays. I am calling the subroutine as:

(@fields, @contents) = flatfile_to_table("databases/myflatfile.dat");

...however when the result is returned, everything that was multi-dimensioned is now single dimensioned.

The subroutine creates an array and using the split operation, divides the data into a multi-dimensional array called @contents. Afterwards and within the same subroutine, I call push to move the first set of data out into another array called @fields (since the first line in a flat file table will contain the field names). This works beautifully. I can traverse all the cells in a row/col fashion (within the subroutine). However, when I do my

return (@fields, @contents)

...and try to work the returned arrays in the main body of my script, the results are single dimensioned arrays.

This has got to be a syntax problem on my part, right? Any ideas?

P.S. Before I shoot myself in th e foot there has to be a free flat file programmer perl lib, right? If you know of one, let me know!

Sean Shrum

(This post was edited by S_Shrum on Dec 29, 2000, 6:39 PM)


Dec 29, 2000, 9:23 PM

Post #2 of 2 (266 views)
Re: passing multidim. arrays out of functions [In reply to] Can't Post

If you don't want your arrays to lose their identity, you need to (ab)use references by doing return( \@fields, \@contents); in your sub, and ($fields, $contents) = flatfile_to_table("..."); (note those are scalars) in your assignment.

Then you can use $fields->[0] to access what would have been $fields[0] or @{ $fields } instead of @fields.


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives