in reply to Read fortran list output

Try this:
use strict; use warnings; use Data::Dumper; my @matrix; while (<DATA>){ chomp; if (m/^[A-Z]/){ push @matrix, [$_]; } else { push @{$matrix[-1]}, $_; } } print Dumper \@matrix; __DATA__ C -2.3242E-003 1.32423 0.34243E+002 ..etc... -3.23134 H more numbers

For each line that begins with an upper case letter it pushes a new array reference onto @matrix, and for all other lines it simply pushes the data onto the last array in @matrix.

If you have to deal more with Fortran output, take a look at Fortran::Format.

Replies are listed 'Best First'.
Re^2: Read fortran list output
by pjotrik (Friar) on Jul 30, 2008 at 10:33 UTC
    I'd use Scalar::Util::looks_like_number in addition:
    #!/usr/bin/perl use warnings; use strict; use Data::Dumper; use Scalar::Util qw(looks_like_number); my $data = []; while (<DATA>) { chomp; if (/^(\w{1,2})$/) { push(@$data, [$1]); } elsif (looks_like_number($_)) { push(@{$$data[-1]}, $_); } } print Dumper($data); __DATA__ C -2.3242E-003 1.32423 0.34243E+002 -3.23134 MD 0.34243E+002 -3.23134
      A nice addition, but you have to take care because not everything that is a number to Fortran is also a number to perl. For example Fortran has output format with a D instead of an E for the exponential.

      Which is why I lazily both recommended Fortran::Format and left the decision on what a number is as an exercise to the reader, who hopefully knows more details about the data format than I do.