#!/usr/bin/perl use strict; use warnings; run(); sub run { my $arr1 = [0 .. 12]; my $arr2 = ['a' .. 'l']; print "before:\n"; print "arr1 = @{$arr1}\n"; print "arr2 = @{$arr2}\n"; # because we're passing references to arrays, any changes # made to the arrays in the subroutine will affect the arrays # here as well. No need to return the arrays. f($arr1, $arr2); # use the arrow operator and square brackets to index the # array, same as in the subroutine below - same syntax, # less confusing $arr1->[7] = "SEVEN!"; print "after:\n"; print "arr1 = @{$arr1}\n"; print "arr2 = @{$arr2}\n"; } sub f { my ($arr1, $arr2) = @_; for (my $i = 0; $i < 5; $i++) { # shuffle some stuff around my $index1 = rand($#$arr1); my $index2 = rand($#$arr2); # use the arrow operator and square brackets to index # the array through a reference my $tmp = $arr1->[$index1]; $arr1->[$index1] = $arr2->[$index2]; $arr2->[$index2] = $tmp; } }