Trivial question : How to print this numbers using Perl ?

Discussion in 'Mac Programming' started by TheBrazilianGuy, Feb 16, 2007.

  1. macrumors regular

    Jul 26, 2006

    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)').


  2. macrumors G3


    Aug 30, 2003
    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?

    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";
  3. thread starter macrumors regular

    Jul 26, 2006
    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,


  4. macrumors regular

    Nov 23, 2006
    why not do this:
    foreach (@A) 
        print $_ " ";
    foreach $index (@A)
        print $index;
    edit - Oops, just realized you wanted columns... I don't think this will help much now.
  5. macrumors G3


    Aug 30, 2003
    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.
  6. thread starter macrumors regular

    Jul 26, 2006
    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 !

  7. macrumors G3


    Aug 30, 2003
    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.
  8. thread starter macrumors regular

    Jul 26, 2006
    I will give a shot with MAP first.
    Thanks a lot !


Share This Page