Print a formatted array

J

Jamie Smyth

I'm having some trouble printing an array in Perl. The array is very
long and must be read in by some archaic FORTRAN code so the spacing
needs to be exact. For those that speak FORTRAN, the format is
basically '(5(F16.10))'.

In perl I have:

105 my $i;
106 for ($i=0; $i<(scalar(@data)); $i++) {
107 my $s = int($i*6);
108 #print "$s $e \n";
109 $fh->printf("%10.6f %10.6f %10.6f %10.6f %10.6f\n",
110 $data[$s], $data[$s+1], $data[$s+2], $data[$s+3],
$data[$s+4]);
111 }

In addition to being rather awful looking and slow, the above code
pads a partially full, last line with zero values if the number of
elements in the array is not divisible by 5.

Is there an easier way?

Thanks.
 
J

Jürgen Exner

Jamie said:
I'm having some trouble printing an array in Perl. The array is very
long and must be read in by some archaic FORTRAN code so the spacing
needs to be exact. For those that speak FORTRAN, the format is
basically '(5(F16.10))'.

In perl I have:

105 my $i;
106 for ($i=0; $i<(scalar(@data)); $i++) {

No need for scalar() here.
107 my $s = int($i*6);

No need for int() here.
You are jumping in steps of 6 elements ...
108 #print "$s $e \n";
109 $fh->printf("%10.6f %10.6f %10.6f %10.6f %10.6f\n",
110 $data[$s], $data[$s+1], $data[$s+2], $data[$s+3],
$data[$s+4]);

.... but you are printing only 5 elements?
In addition to being rather awful looking and slow, the above code

Idea #1: use a better data structure! What you got there is in reality a
two-dimensional array. So use one. With an array of arrays your code will
become a lot nicer.
pads a partially full, last line with zero values if the number of
elements in the array is not divisible by 5.

If you want to go with a c-style loop (and in this case it might actually
not be that bad) then what about:

for my ($i=5; $i < @data; $i += 6) {
$fh->printf("%10.6f %10.6f %10.6f %10.6f %10.6f\n",
@data[$i-5..$i-1]); #not sure if the array slice works here
}

This will terminate before trying to print incomplete records.

jue
 
A

Anno Siegel

Jamie Smyth said:
I'm having some trouble printing an array in Perl. The array is very
long and must be read in by some archaic FORTRAN code so the spacing
needs to be exact. For those that speak FORTRAN, the format is
basically '(5(F16.10))'.

In perl I have:

105 my $i;
106 for ($i=0; $i<(scalar(@data)); $i++) {
107 my $s = int($i*6);
^
should be 5, no?
108 #print "$s $e \n";
109 $fh->printf("%10.6f %10.6f %10.6f %10.6f %10.6f\n",
110 $data[$s], $data[$s+1], $data[$s+2], $data[$s+3],
$data[$s+4]);
111 }

In addition to being rather awful looking and slow, the above code
pads a partially full, last line with zero values if the number of
elements in the array is not divisible by 5.

If you can afford to destroy the original array:

$_ = sprintf( '%10.6f', $_) for @array;
print( join( ' ', splice @array, 0, 5), "\n") while @array;

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,404
Latest member
PerryRutt

Latest Threads

Top