Dear Mike,

There are many benefits of using references. One such advantage is when you
pass two arrays to a subroutine.

I will illustrate with an example which shows how you can pass two arrays
to a subroutine. The first example explains passing arrays without
references and the second example explains passing arrays using references.

[code]
#Subroutine which uses two arrays;
#Result : Combines both arrays into one (First array slurps out the
remaining array)
sub world_cup_team_1 {
my(@team_india, @team_westindies) = @_;
print "Team India : ", (join ',', @team_india), "\n";
#Adding the player 'tendulkar' into @team_india but it will not get added
#to the original array outside this subroutine
push @$team_india, "tendulkar";

print "Team West Indies : ", (join ',', @team_westindies), "\n";
}

#Subroutine which uses two arrays;
#Result : Get tow array references into two separate scalar variables
sub world_cup_team_2 {
my($team_india, $team_westindies) = @_;
print "Team India : ", (join ',', @$team_india), "\n";
#Adding the player 'tendulkar' into @team_india but it will get added
#to the original array outside this subroutine
push @$team_india, "tendulkar";

print "Team West Indies : ", (join ',', @$team_westindies), "\n";
}

my @india = qw/dhoni raina sehwag kumble/;
my @westindies = qw/lara richards walsh ambrose/;

print '-' x 40, "\n";
print "Example : Without using references\n";
world_cup_team_1 (@india, @westindies);
print "Team India (Values after calling the subroutine without using
references) : ", (join ',', @india), "\n";
print '-' x 40, "\n";

print '-' x 40, "\n";
print "Example : Using references\n";
world_cup_team_2 (\@india, \@westindies);
print "Team India (Values after calling the subroutine using references) :
", (join ',', @india), "\n";
print '-' x 40, "\n";
[/code]

[output]
----------------------------------------
Example : Without using references
Team India : dhoni,raina,sehwag,kumble,lara,richards,walsh,ambrose
Team West Indies :
Team India (Values after calling the subroutine without using references) :
dhoni,raina,sehwag,kumble
----------------------------------------
----------------------------------------
Example : Using references
Team India : dhoni,raina,sehwag,kumble
Team West Indies : lara,richards,walsh,ambrose
Team India (Values after calling the subroutine using references) :
dhoni,raina,sehwag,kumble,tendulkar
----------------------------------------
[/output]

As you can see in the second example, a new player 'tendulkar' is added to
the original array in this case '@india' besides getting stored in its own
array; whereas, in the first example the first array slurps all elements
leaving nothing to the second array (ie to Team West Indies). All the array
functions such as shift, unshift, push, pop uses references which will
affect the original array.

Hope it helps.


On Wed, May 14, 2014 at 11:01 AM, Mike Dunaway <ekimduna...@gmail.com>wrote:

> What's a good use of references? When is it ideal to use them? Why would
> you want to use them?
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


-- 
best,
Shaji
----------------------------------------------------------------------------------------------------------------------------------------------
Your talent is God's gift to you. What you do with it is your gift back to
God.
----------------------------------------------------------------------------------------------------------------------------------------------

Reply via email to