in reply to How has Perl saved you time at your job this week|month|year|decade?

I spent some time porting a Fortran application to C++. That was quite error-prone, so I had to compare intermediate values in the process, which were all large matrices of complex numbers. Comparing them is boring, takes long and is error-prone in itself, so I wrote a script which compares those matrices for me.

It's really a hack, but it works fine for me. Now I also use it to test my C++ application when I change internals that shouldn't affect the output.

#!/usr/bin/perl use strict; use warnings; use Math::Complex; use Data::Dumper; sub read_file { # we ignore that the structure is a matrix, and just read it as if + where a # list my $fn = shift; open my $file, '<', $fn or die "Can't open file '$fn' for reading: + $!"; my @list; my $contents = do { local $/; <$file> }; close $file or warn $!; my $number_re = qr{ \( \s* ([^\s(),]*) # real part \s*,\s* ([^\s(),]*) # imaginary part ,? \s* \) }x; while ($contents =~ m/$number_re/g){ push @list, $1 + $2 * i; } return @list; } my @a = read_file($ARGV[0] || 'grinv-cpp'); my @b = read_file($ARGV[1] || 'grinv-nano'); print scalar(@a), "\t", scalar(@b), "\n"; my $n = @a; for (0..$n - 1) { my $diff = abs($a[$_] - $b[$_]); if ($diff > 1e-4) { printf "Index %d (%d, %d): got %s, expected %s\n", $_, $_ % sqrt($n), int($_ / sqrt($n)), $a[$_], $b[$_]; } } # vim: ft=perl expandtab ts=4 sw=4

Actually I wrote quite many small scripts for similar tasks: converting matrices from one format to another one so it can be used as input by a different library, sanity check (check symmetries in my output matrices) etc, this is just the most recent example.

All of them saved much time, either my own or that of co-workers.

  • Comment on Re: How has Perl saved you time at your job this week|month|year|decade?
  • Download Code