Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

TheBrazilianGuy

macrumors regular
Original poster
Jul 26, 2006
149
0
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

macrumors G3
Aug 30, 2003
8,634
0
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?

Code:
#!/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

macrumors regular
Original poster
Jul 26, 2006
149
0
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?

Code:
#!/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

macrumors regular
Nov 23, 2006
109
0
why not do this:
Code:
foreach (@A) 
{
    print $_ " ";
}

Or
Code:
foreach $index (@A)
{
    print $index;
}

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

iMeowbot

macrumors G3
Aug 30, 2003
8,634
0
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.
 

TheBrazilianGuy

macrumors regular
Original poster
Jul 26, 2006
149
0
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.
 

iMeowbot

macrumors G3
Aug 30, 2003
8,634
0
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:

Code:
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

macrumors regular
Original poster
Jul 26, 2006
149
0
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:

Code:
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.