By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,852 Members | 1,426 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,852 IT Pros & Developers. It's quick & easy.

Passing interior array of two-dimensional array to subroutine

P: 5
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
Share this Question
Share on Google+
6 Replies


Ganon11
Expert 2.5K+
P: 3,652
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
Expert 2.5K+
P: 4,059
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

P: 5
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
Expert 2.5K+
P: 3,652
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
Expert 2.5K+
P: 4,059
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

P: 5
Gotcha. Thank you both again.

John
Apr 1 '08 #7

Post your reply

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