srkaeppler has asked for the wisdom of the Perl Monks concerning the following question:

Hey all- I am new here for starters. I had a project where i need to write code in order to format certain astronomical databases for use later on in observation.

so here is an example of what the output should look like
233 NGC 188 - 233 0 38 40.024 85 00 21.93 F STATUS=OK

this is an example of what my code produces
12 NGC 188 - 12 6 6 31.217 85 33 30.70 O STATUS=OK
now what is important here is simply the numbers in the
middle which indicate Right Accension and Declination
the format output should look like (for example)
6 06 31.217 85 33 30.70
that zero attached to the front of the six is my problem, in my code, i am not sure how to make it so that way instead of a single digit _6(_ indicates a space), i can get an 06.

so here is the important code

while ($inline = <INPUT>) { #INPUT defined as variable# @firstline = split(/\s+/, $inline); #firstline of array defined by sp +lit of white space into variable inline# $hms1 = $firstline[2]; $hh1 = int ($hms1); $mm1 = int (60*( abs($hms1) - abs($hh1) ) ); $ss1 = 3600*abs($hms1) - 3600*abs($hh1) - 60*abs($mm1); $hms2 = $firstline[3]; $hh2 = int($hms2); $mm2 = int(60*( abs($hms2) - abs($hh2) ) ); $ss2 = 3600*abs($hms2) - 3600*abs($hh2) - 60*abs($mm2); $obid = "NGC 188 - $firstline[1]"; write; } close INPUT; format STDOUT = @>>> @<<<<<<<<<<<<<<<<<<< @# @# @#.### @# @# @#.## @ @<<<<<<<< $firstline[1], $obid, $hh1, $mm1, $ss1, $hh2, $mm2, $ss2, $cfo, $statu +s .
The there are three things happening in the loop. the loops involving converting into the variables $mm1, $ss1, etc creating the numbers that will go into the final format below. Again my question is where would i change my code so that way i could print out 06 instead of just _6(where _ indicates a space)?

I hope this makes sense what i am asking, if it does not, please let me know and i will attempt to reword it a little bit better.

thank you very much!
Steve

Replies are listed 'Best First'.
Re: Formating question
by davido (Cardinal) on Aug 11, 2004 at 16:17 UTC

    You might have a look in the Perl POD under perlfunc -f printf and perlfunc -f sprintf. Therein you'll find your answer, if you're willing to rely on string formatting rather than Perl formats. ;)


    Dave

Re: Formating question
by idsfa (Vicar) on Aug 11, 2004 at 16:19 UTC

    From perldoc perlform:

    With a "0" (zero) instead of the first "#", the formatted number will be padded with leading zeroes if necessary.

    So change your format to

    format STDOUT = @>>> @<<<<<<<<<<<<<<<<<<< @# @0 @#.### @# @0 @#.## @ @<<<<<<<<

    If anyone needs me I'll be in the Angry Dome.
Re: Formating question
by Roger (Parson) on Aug 11, 2004 at 16:20 UTC
    Do you really have to use format? I would rather use a simple printf instead:
    printf "%d %02d\n", 1, 2;
    This prints out
    1 02

    Where %02d denotes a 2 digit number with leading zero.

Re: Formating question
by Old_Gray_Bear (Bishop) on Aug 11, 2004 at 16:28 UTC
    It looks like you can't do that with '#' in a format. Sigh.

    So (There Is More Than One Way ... after all), we use sprintf() and pre-format our field.

    my $hh1_char = sprintf("%02d", $hh1); my $mm1_char = sprintf("%02d", $mm1); my $ss1_char = sprintf("%02d", $ss1);
    Then use $hh1_char, etc, in your format list. (Changing the appropriate '#'s to '<'s, of course.)

    The sprintf() format ("%02d") says we are going to have decimal digits, two of them, and fill any leading spaces with zeros please.

    ----
    I Go Back to Sleep, Now.

    OGB