473,382 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Passing interior array of two-dimensional array to subroutine

Hello,

I've made a two dimensional array using references, which I gather is the only way to do it in Perl. I now need to pass each interior array to a subroutine for processing and can't quite work out the syntax. Here's what I have so far. As you can see, my understanding of Perl falls apart when I get to the subroutine. Any help would make my day. Thanks.

Expand|Select|Wrap|Line Numbers
  1. my @fruitFiles =("apple.html", "orange.html", "kiwi.html");
  2. my @vegetableFiles =("pea.html", "carrot.html", "limabean.html"); 
  3. my @breadFiles =("rye.html", "wheat.html", "sevenGrain.html"); 
  4.  
  5.  
  6. #Make references to arrays.
  7. my $fruitArray = \@fruitFiles; 
  8. my $vegArray = \@vegetableFiles;
  9. my $breadArray = \@breadFiles;
  10.  
  11.  
  12. # Put references into an array to make a two-dimensional array.
  13. @categoryArrays = ($fruitArray, $vegArray, $breadArray);
  14.  
  15. #Proof of concept, debug.
  16. for my $i (0..$#categoryArrays) {
  17.    for my $j (0..$#{$categoryArrays[$i]}) {            
  18.       print "\$i = $i, \$j = $j, $categoryArrays[$i][$j]\n";
  19.    }
  20. }
  21.  
  22.  
  23. for my $q (0..$#categoryArrays) {
  24.     #pass each array to subroutine.
  25.    &buildList ($categoryArrays[$q]);  
  26.    }
  27.  
  28.  
  29. sub buildList($catArray)
  30. {
  31. @locArray = $catArray;
  32. foreach $element (@locArray)
  33. {
  34. print $element;
  35. print "\n";
  36. # I actually want to open all of the files in the passed array, manipulate and concatenate bits of them into an output file.
  37. # I think I can manage that part if I can just reference them correctly down here.
  38. }
  39.  
  40. }
  41.  
  42.  
Mar 28 '08 #1
6 3675
Ganon11
3,652 Expert 2GB
Hello,

I've made a two dimensional array using references, which I gather is the only way to do it in Perl. I now need to pass each interior array to a subroutine for processing and can't quite work out the syntax. Here's what I have so far. As you can see, my understanding of Perl falls apart when I get to the subroutine. Any help would make my day. Thanks.

Expand|Select|Wrap|Line Numbers
  1. #Make references to arrays.
  2. my $fruitArray = \@fruitFiles; 
  3. my $vegArray = \@vegetableFiles;
  4. my $breadArray = \@breadFiles;
  5.  
  6.  
  7. # Put references into an array to make a two-dimensional array.
  8. @categoryArrays = ($fruitArray, $vegArray, $breadArray);
  9.  
  10. for my $q (0..$#categoryArrays) {
  11.     #pass each array to subroutine.
  12.    &buildList ($categoryArrays[$q]);  
  13.    }
  14.  
  15.  
  16. sub buildList($catArray)
  17. {
  18. @locArray = $catArray;
  19. foreach $element (@locArray)
  20. {
  21. print $element;
  22. print "\n";
  23. # I actually want to open all of the files in the passed array, manipulate and concatenate bits of them into an output file.
  24. # I think I can manage that part if I can just reference them correctly down here.
  25. }
  26.  
  27. }
  28.  
  29.  
All right, so inside buildList, we assume that you were passed in a reference to an array. (I'm not sure what the parentheses syntax is you've used - I haven't come across that in my studies). So @_ (the array with all of the arguments passed to the current subroutine) should have 1 element - the reference. You want locArray to be the array that is referenced - I can see that, because you use a foreach loop to access all members. But what you have now:

Expand|Select|Wrap|Line Numbers
  1. @locArray = $catArray;
means @locArray will be an array with 1 element - the reference you were passed in. If you want to get the array, you need to dereference the reference:

Expand|Select|Wrap|Line Numbers
  1. @locArray = @{$catArray};
Now @locArray is an array with the same contents as the array referenced by $catArray.

As an aside, why aren't you using my to declare these variables as lexicals? i.e. shouldn't it be:

Expand|Select|Wrap|Line Numbers
  1. sub buildList($catArray)
  2. {
  3.    # I think this should actually be:
  4. # sub buildList
  5. # {
  6. #    my @locArray = @{$_[0]};
  7.    my @locArray = @{$catArray};
  8.    foreach my $element (@locArray)
  9.    {
  10.       print $element;
  11.       print "\n";
  12.    }
  13.  
  14. }
You might have perfectly good reasons for doing so that I don't understand (after all, I'm taking my first class in Perl right now, so I don't have a lot of experience).
Mar 28 '08 #2
KevinADC
4,059 Expert 2GB
I did not go over all the code, but change the buildList function to this:

Expand|Select|Wrap|Line Numbers
  1. sub buildList
  2. {
  3. my $locArray = shift; # imports the data into the function 
  4. foreach $element (@{$locArray})
  5. {
  6. print $element;
  7. print "\n";
  8. # I actually want to open all of the files in the passed array, manipulate and concatenate bits of them into an output file.
  9. # I think I can manage that part if I can just reference them correctly down here.
  10. }
  11.  
  12. }
Ask questions if you have any.
Mar 28 '08 #3
Thank you both! Kevin, yours worked well; Gannon, thanks for the tips and thoughtful walk-through. I never took coursework in Perl so I have gaping holes in my knowledge and, I forget e.g., about "my".
Can either of you tell me how in the subroutine I can retrieve the name of the array variable I passed in? I need to be able to refer to the literal, "fruitArray".
Thanks.
John
Mar 28 '08 #4
Ganon11
3,652 Expert 2GB
You could add its name to the front of the actual array, or pass it into the subroutine as an additional variable. Otherwise, there is no way - remember that a variable's name is only for us programmers to comprehend and has nothing to do with the actual value that gets passed around.
Mar 29 '08 #5
KevinADC
4,059 Expert 2GB
Thank you both! Kevin, yours worked well; Gannon, thanks for the tips and thoughtful walk-through. I never took coursework in Perl so I have gaping holes in my knowledge and, I forget e.g., about "my".
Can either of you tell me how in the subroutine I can retrieve the name of the array variable I passed in? I need to be able to refer to the literal, "fruitArray".
Thanks.
John
You need to rethink your premise, there should never be a need to retrieve an array name in a perl program. You could pass it as a string to your function as Ganon11 suggests.
Mar 29 '08 #6
Gotcha. Thank you both again.

John
Apr 1 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: dzieciou | last post by:
I've used JENA and got the following result in result of query to RDF file: <j.0:ResultSet> <j.0:solution rdf:parseType="Resource"> <j.0:binding rdf:parseType="Resource"> <j.0:value>John...
2
by: Catherine Lynn Wood | last post by:
I need to know how to overlap DIV content within 'relative' associated rendering. I am building div layers in the middle of a page and when I set positioning to absolute in the CSS, it references...
25
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default...
8
by: Dennis Myrén | last post by:
I have these tiny classes, implementing an interface through which their method Render ( CosWriter writer ) ; is called. Given a specific context, there are potentially a lot of such objects,...
17
by: LP | last post by:
Hello, Here's the scenario: Object A opens a Sql Db connection to execute number of SqlCommands. Then it needs to pass this connection to a constructor of object B which in turn executes more...
22
by: Arne | last post by:
How do I pass a dataset to a webservices? I need to submit a shoppingcart from a pocket PC to a webservice. What is the right datatype? II have tried dataset as a datatype, but I can't get it to...
10
by: Stan | last post by:
There are two ways to pass structured data to a web service: xml === <Order OrderId="123" OrderAmount="234" /> or class =====
1
by: quat | last post by:
I have two unmanaged pointer in a managed form class: IDirect3D9* d3dObject; IDirect3DDevice9* d3dDevice; In a member function of the form, I call: d3dObject->CreateDevice(...
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
7
by: AMP | last post by:
Hello, I have this in form1: namespace Pass { public partial class Form1 : Form { public Form2 form2; public Form1() {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.