in reply to Re^2: Joining separate data files to make one.
in thread Joining separate data files to make one.

The multiple calls to map have me perplexed.

Sure. map is abused here to work as foreach and while.

map { open my $file, '<', $_ or die $!; map { $merged{$_->[0]} //= [ qw{null} x 3 ]; $merged{$_->[0]}[$index] = $_->[1]; } map { [ m{ \A ( \S+ \s \S+ \s \S+ \s \S+ ) \s ( \S+ ) \z }msx ] } map { chomp; $_ } (<$file>); close $file; ++$index; } qw{gravity magnetics bathymetry};

The outer map is really a foreach:

foreach my $filename (qw{gravity magnetics bathymetry}) { open my $file, '<', $filename or die $!; map { $merged{$_->[0]} //= [ qw{null} x 3 ]; $merged{$_->[0]}[$index] = $_->[1]; } map { [ m{ \A ( \S+ \s \S+ \s \S+ \s \S+ ) \s ( \S+ ) \z }msx ] } map { chomp; $_ } (<$file>); close $file; ++$index; }

The last map inside the foreach loop simply iterates over all lines of the file and strips trailing newlines. Then it passes each line to the middle map, which extracts some parts of the line, and returns an array reference with the matches. The first map is again abused as a foreach.

Using while (<$file>) would make that more readable:

foreach my $filename (qw{gravity magnetics bathymetry}) { open my $file, '<', $filename or die $!; while (<$file>) { chomp; my @a=m{ \A ( \S+ \s \S+ \s \S+ \s \S+ ) \s ( \S+ ) \z }msx; $merged{$a[0]}//=[ qw{null} x 3 ]; $merged{$a[0}}[$index]=$a[1]; } close $file; ++$index; }

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)