Passing interior array of two-dimensional array to subroutine

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • johntology
    New Member
    • Nov 2007
    • 5

    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.

    [CODE=perl]
    my @fruitFiles =("apple.html ", "orange.htm l", "kiwi.html" );
    my @vegetableFiles =("pea.html", "carrot.htm l", "limabean.html" );
    my @breadFiles =("rye.html", "wheat.html ", "sevenGrain.htm l");


    #Make references to arrays.
    my $fruitArray = \@fruitFiles;
    my $vegArray = \@vegetableFile s;
    my $breadArray = \@breadFiles;


    # Put references into an array to make a two-dimensional array.
    @categoryArrays = ($fruitArray, $vegArray, $breadArray);

    #Proof of concept, debug.
    for my $i (0..$#categoryA rrays) {
    for my $j (0..$#{$categor yArrays[$i]}) {
    print "\$i = $i, \$j = $j, $categoryArrays[$i][$j]\n";
    }
    }


    for my $q (0..$#categoryA rrays) {
    #pass each array to subroutine.
    &buildList ($categoryArray s[$q]);
    }


    sub buildList($catA rray)
    {
    @locArray = $catArray;
    foreach $element (@locArray)
    {
    print $element;
    print "\n";
    # I actually want to open all of the files in the passed array, manipulate and concatenate bits of them into an output file.
    # I think I can manage that part if I can just reference them correctly down here.
    }

    }

    [/CODE]
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Originally posted by johntology
    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.

    [CODE=perl]#Make references to arrays.
    my $fruitArray = \@fruitFiles;
    my $vegArray = \@vegetableFile s;
    my $breadArray = \@breadFiles;


    # Put references into an array to make a two-dimensional array.
    @categoryArrays = ($fruitArray, $vegArray, $breadArray);

    for my $q (0..$#categoryA rrays) {
    #pass each array to subroutine.
    &buildList ($categoryArray s[$q]);
    }


    sub buildList($catA rray)
    {
    @locArray = $catArray;
    foreach $element (@locArray)
    {
    print $element;
    print "\n";
    # I actually want to open all of the files in the passed array, manipulate and concatenate bits of them into an output file.
    # I think I can manage that part if I can just reference them correctly down here.
    }

    }

    [/CODE]
    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:

    [CODE=perl]@locArray = $catArray;[/CODE]

    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:

    [CODE=perl]@locArray = @{$catArray};[/CODE]

    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:

    [CODE=perl]sub buildList($catA rray)
    {
    # I think this should actually be:
    # sub buildList
    # {
    # my @locArray = @{$_[0]};
    my @locArray = @{$catArray};
    foreach my $element (@locArray)
    {
    print $element;
    print "\n";
    }

    }[/CODE]

    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).

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      I did not go over all the code, but change the buildList function to this:

      [CODE=perl]sub buildList
      {
      my $locArray = shift; # imports the data into the function
      foreach $element (@{$locArray})
      {
      print $element;
      print "\n";
      # I actually want to open all of the files in the passed array, manipulate and concatenate bits of them into an output file.
      # I think I can manage that part if I can just reference them correctly down here.
      }

      }[/CODE]

      Ask questions if you have any.

      Comment

      • johntology
        New Member
        • Nov 2007
        • 5

        #4
        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

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          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.

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            Originally posted by johntology
            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.

            Comment

            • johntology
              New Member
              • Nov 2007
              • 5

              #7
              Gotcha. Thank you both again.

              John

              Comment

              Working...