"Stout, Joel R" wrote:
> 
> Ok I know what it does ('cause I ran it, see below) but I still don't fully
> understand how.  Also can you give a little insight into passing arrays to
> subroutines/functions.  I can pass them alright but have problems accessing
> them.  I use $_[0] but it doesn't seem to work for arrays. Any help would be
> appreciated.

Passing arrays to subroutines....

Basically, the values coming into a subroutine can be accessed using @_.

Here's a simple example that just prints the values passed into the
routine:


   arrayRoutine {
      my @array = @_;

      foreach( @array ) {
         print "$_\n";
      }
   }

Of course, you could have used @_ instead of creating a new copy
(@array).  In that case, $_[1] should have worked (what message did you
get or can you explain why it didn't seem to work for you?).

When you pass anything to a subroutine, it gets passed as a single
array.  So if you made a call like

   arrayRoutine( @x, @y );

when arrayRoutine gets the data, @array will contain the values of both
@x and @y.  In this way, there's no way to distinguish between @x and @y
once they get to the routine.  Same thing for hashes (since hashes are
just a variation of arrays).  When I was starting out I thought that
this sucked.  But then I learned about references and a whole new world
opened to me ~8^)

A reference is a SCALAR type that points to the other entity.  When I
was learning C pointers the correlation to a library helped me a lot. 
In a library there is a card catalog (do people still go to the library
these days ~8^)  You find an item you want to get (book, video, tape,
etc.) and look it up in the card catalog.  You get the reference number
to that item.  Because you understand how to find your way around the
shelves, you can easily find that item because you have that reference
number.  Note that it doesn't really matter what the item is.  As long
as you have that reference number you can find what ever it is.

That's how references work.  The variable containing the SCALAR
reference to the object is like the card catalog.  It's something that
you have named yourself so you have easy access to it.  The reference
number is the value of that variable.  You rarely need to look at the
value of the variable.  It's more for Perl's internal workings.  Perl
does all the fumbling through the shelves to find the object you need.

To get a reference to an object in Perl you use the back slash (\). 
What you get back is the reference.

  my $array1_ref = \@array1;
  my $array2_ref = \@array2;

  arrayRoutine( $array1_ref, $array2_ref );

Now when arrayRoutine gets the array, there are 2 SCALAR elements: the
references to the 2 arrays.  We could have also called arrayRoutine this
way:

  arrayRoutine( \@array1, \@array2 );

It's the same thing as far as arrayRoutine is concerned.

Now, if you left arrayRoutine as it is above, the output would be
something like

     ARRAY(0x80561c8)
     ARRAY(0x80562a0)

These funny looking things are the values of the SCALAR references. 
These are the actual numbers you looked up in the card catalog.  You can
see they aren't very pretty and you probably won't need to use them. 
But Perl needs them to know where the values in those arrays you
referenced can be found.

To dereference a reference varies by type.  If the reference is an array
you use @ if it's a hash you use % if it's a SCALAR you use $.  Nothing
really new, right.  So, to get to the array, you could do something like

    @{$array1_ref}

The arrayRoutine could be rewritten like this

     sub arrayRoutine {
        my( $array1_ref, $array2_ref ) = @_;

        print "First array\n";
        foreach( @{$array1_ref} ) {
           print "$_\n";
        }
        print "Second array\n";
        foreach( @{$array2_ref} ) {
           print "$_\n";
        }
     }

Once you get the idea of references, you can create very complex data
structures (hashes of arrays of hashes of hashes of arrays).

Dan

Reply via email to