# Rather simple Perl time test. # Builds and sorts array(s) of numbers. sub make_num_list($) { $howmany = $_[0]; @mylist = (); $i, $j, $k = 20, 245, 8003; #$i = 20; #$j = 245 #$k = 8003; while ($howmany > 0) { push(@mylist, ($i % 20) + ($j * 4023 % 145) + ($k * 5671 % 879) ); $i += 1; $j += 1; $k += 1; #print mylist; --$howmany; } return @mylist; } # sub make_num_list # For "simplicity," this is based on the Perl Cookbook (May 1999 Ed.) # (Chapter 10: Subroutines -- 10.5 Passing Arrays and Hashes by Reference). # Unfortunately, "simplicity" doesn't work very well. This IS Perl after all. # I am returning and assigning @times *not* to encourage Perl to be inefficient, # but rather because if I don't return and assign the passed-by-value array, # Perl tends to perform bizarre vandalizations upon the originally referenced array # in the calling code, rather than just modifying the referenced array as desired. # As for efficiency, because we have only about 4 items in the array, it's no big deal here. sub add_a_time (\@) { my $times = $_[0]; #push @times, 45; push (@$times, time()); return @$times; } # sub add_a_time sub num_list_test($) { my $howmany = $_[0]; @times = (); @times = add_a_time(@times); print 'RUNNING FUNCTION num_list_test(', $howmany, ")\n"; print @times, "\n"; #foreach $k (@times) { print $k, "\n"; } print 'times : ', scalar(join(', ', @times)) , "\n"; @mylist = make_num_list($howmany); @times = add_a_time(@times); #print scalar(join(', ', @mylist)), "\n"; @times = add_a_time(@times); #@mylist = sort(@mylist); # Gives us alphabetically sorted, not what we want for numbers. #@mylist = sort({ return $a <=> $b; }, @mylist); # 'Obvious' syntax error. @mylist = sort{ return $a <=> $b; } @mylist; # Actually does numeric sort. @times = add_a_time(@times); print scalar(join(', ', @mylist)), "\n"; @times = add_a_time(@times); print 'times : ', scalar(join(', ', @times)) , "\n"; print 'EXITING FUNCTION num_list_test(', $howmany, ")\n\n"; } # sub num_list_test num_list_test(5); num_list_test(500); num_list_test(5000); num_list_test(50000); num_list_test(100000); # -- This provides a marvelous example of references confusion, or maybe multiple. -- =cut # For "simplicity," this is based on the Perl Cookbook (May 1999 Ed.) # (Chapter 10: Subroutines -- 10.5 Passing Arrays and Hashes by Reference). # Unfortunately, "simplicity" doesn't work very well. This IS Perl after all. sub add_a_time (\@) { my $times = $_[0]; #push @times, 45; push (@$times, 45); } # sub add_a_time @times = (0, 90); add_a_time(\@times); print @times, "\n"; foreach $k (@times) { print $k, "\n"; } print 'times : ', scalar(join(', ', @times)) , "\n"; =thecrap # -- This provides a marvelous example of references confusion, or maybe multiple. -- #=cut sub add_a_time (\@) { @times = $_[0]; push @times, 45; #push @_, 45; } # sub add_a_time @times = (0, 90); add_a_time(@times); # Commenting out or uncommenting out this function call # changes @times' type in the future. print @times, "\n"; foreach $k (@times) { print $k, "\n"; } print 'times : ', scalar(join(', ', @times)) , "\n"; #=thecrap