PDA

View Full Version : Trivial question : How to print this numbers using Perl ?




TheBrazilianGuy
Feb 16, 2007, 03:43 PM
Hi,

Let's say you have 150 integer numbers. How can you print all of then in a single line ?

I know how to do this in Fortran using something like WRITE(2,'(150E12.4)').

Thxs,

D.



iMeowbot
Feb 16, 2007, 06:28 PM
I'm not sure I know what you mean. Are you looking for something like the implied DO in Fortran?

Is one of these what you want?


#!/usr/bin/perl
use strict;

# for an array, this example will print '1.5 1500000000000004 3.3 4 0.005'
my @a=(1.5, 150E12.4, 3.3 , 4, 0.005);
print "@a","\n";

#for some list of numbers, this example prints '14 33.77 4.1414'
print map({"$_ "} 14, 33.77, 4.1414), "\n";

TheBrazilianGuy
Feb 16, 2007, 07:49 PM
Thanks for the reply, iMeowbot.

No, it was not what I meant. I have a vector (or hash, if you prefer) with 150 numbers on it. Something like

A[1] = 0
A[2] = 1032
...
A[150] = 2012

I have to print all elements of A[] in the same line using the same format.
In Fortran, this is trivial as you just need to put the number of elements
in front of your selected output format :

150I6 (print 150 integers using 6 columns)

Today I saw that Perl has something similar with "x" :

$a = number #set variable
print $a x 100 #repeat word "number" 100 times.

but I do not know how to set the format using "x".
Do you know if this work :

$a = variable
printf "%d " x 100, @A

Thanks again,

D.

I'm not sure I know what you mean. Are you looking for something like the implied DO in Fortran?

Is one of these what you want?


#!/usr/bin/perl
use strict;

# for an array, this example will print '1.5 1500000000000004 3.3 4 0.005'
my @a=(1.5, 150E12.4, 3.3 , 4, 0.005);
print "@a","\n";

#for some list of numbers, this example prints '14 33.77 4.1414'
print map({"$_ "} 14, 33.77, 4.1414), "\n";

bronxbomber92
Feb 16, 2007, 08:17 PM
why not do this:
foreach (@A)
{
print $_ " ";
}

Orforeach $index (@A)
{
print $index;
}

edit - Oops, just realized you wanted columns... I don't think this will help much now.

iMeowbot
Feb 16, 2007, 09:09 PM
If you really want to use that kind of formatting (not just one row, but full Fortran-style formatting with multi-row looping), it may be easier for you to install and use the Fortran::Format module (http://search.cpan.org/~itub/Fortran-Format-0.90/).

TheBrazilianGuy
Feb 16, 2007, 10:14 PM
Thanks for the replies.

Well, this problem did not look so impossible at first.
You see, the problem is to print a single line with
a bunch of numbers spaced by an space. My original fortran
code handles a three dimensional matrix GRID

WRITE(2,'(8000000E15.6)') (((grid(celx,cely,celz),celx=limitA,limitB),
cely=limitA,limitB), celz=limitA,limitB)

but then I needed to use a Perl code instead.
That's when I got stuck with this formatted print command.

Well, I will see what else can be done.

Thanks again !

If you really want to use that kind of formatting (not just one row, but full Fortran-style formatting with multi-row looping), it may be easier for you to install and use the Fortran::Format module (http://search.cpan.org/~itub/Fortran-Format-0.90/).

iMeowbot
Feb 16, 2007, 10:26 PM
TWell, this problem did not look so impossible at first. You see, the problem is to print a single line with
a bunch of numbers spaced by an space.
The examples I posted earlier in the thread do exactly that. You can use map to apply formatting as so:


my @a=1..297;

print map { sprintf("%05d ", $_) } @a;


This isn't hard.


My original fortran
code handles a three dimensional matrix GRID

WRITE(2,'(8000000E15.6)') (((grid(celx,cely,celz),celx=limitA,limitB),
cely=limitA,limitB), celz=limitA,limitB)

And this can be done with simple for (like in C) or do looping.

TheBrazilianGuy
Feb 16, 2007, 11:16 PM
Awesome.
I will give a shot with MAP first.
Thanks a lot !

The examples I posted earlier in the thread do exactly that. You can use map to apply formatting as so:


my @a=1..297;

print map { sprintf("%05d ", $_) } @a;


This isn't hard.




And this can be done with simple for (like in C) or do looping.