Messing with Perl


sub zipper {
my ($left, $right) = @_;
my @larr = @{$left};
my $lsize = @larr;

my @rarr = @{$right};
my $rsize = @rarr;

if ($lsize != $rsize) {

die "Array sizes are inequal.";

}



my @AoAr;

for (my $i = 0; $i < $lsize; $i++) {
push @AoAr, ($larr[$i], $rarr[$i]), "n";
}

return @AoAr;

}

Earlier I decided to make a zipper subroutine in Perl. So far it works. Results:


$VAR1 = 'Pat';
$VAR2 = '1';
$VAR3 = '
';
$VAR4 = 'Sean';
$VAR5 = '2';
$VAR6 = '
';
$VAR7 = 'Dante';
$VAR8 = '3';
$VAR9 = '
';
$VAR10 = 'Dustin';
$VAR11 = '4';
$VAR12 = '
';

I also devised a dot product using two maps:


my @dotproduct = map {my $z = $_; map {$z . ' ' . $_}
@surnames} @names;

Results:


$VAR1 = 'Pat 1';
$VAR2 = 'Pat 2';
$VAR3 = 'Pat 3';
$VAR4 = 'Pat 4';
$VAR5 = 'Sean 1';
$VAR6 = 'Sean 2';
$VAR7 = 'Sean 3';
$VAR8 = 'Sean 4';
$VAR9 = 'Dante 1';
$VAR10 = 'Dante 2';
$VAR11 = 'Dante 3';
$VAR12 = 'Dante 4';
$VAR13 = 'Dustin 1';
$VAR14 = 'Dustin 2';
$VAR15 = 'Dustin 3';
$VAR16 = 'Dustin 4';

Another fine example of bad programming. Now here are some things I should have learned way earlier:

  • Lists are not the same as arrays. Arrays are filled with scalars. Lists have objects of some sort.
  • Lists don’t really have a scalar context. It resolves to their last element.
  • Changing from an array reference to an array is way too clunky. Is there a better way?

Leave a Reply

Your email address will not be published. Required fields are marked *