Adding leading zeros to variable data

I'm sure there's a simple solution to my problem but not programing full time I'm not as learned as I'd like to be. I'm working on a script that grabs Airport weather data using Geo::METAR and formats it into a telemetry packet. Now a few of the fields are outputed from Geo::METAR in variable lengths because of changing weather conditions ie. 5 or 20 Mile per hour wind speeds. what I need to do is add leading zeros on so I have three bytes in the field once the script outputs the data ie 005 or 020.
Like I said I'm sure there's an easy way to do it but it escapes me..

My out put packet currently looks like this:
CYPE>APRS,TCPIP*,qAC,APRSFL:@100232z5614.00N/11726.00W_160/[b]3[/b]g...t057b10172

It should look like this:
CYPE>APRS,TCPIP*,qAC,APRSFL:@100232z5614.00N/11726.00W_160/[b]003[/b]g...t057b10172
Sean

Comments

  • : I'm sure there's a simple solution to my problem but not programing
    : full time I'm not as learned as I'd like to be. I'm working on a
    : script that grabs Airport weather data using Geo::METAR and formats
    : it into a telemetry packet. Now a few of the fields are outputed
    : from Geo::METAR in variable lengths because of changing weather
    : conditions ie. 5 or 20 Mile per hour wind speeds. what I need to do
    : is add leading zeros on so I have three bytes in the field once the
    : script outputs the data ie 005 or 020.
    : Like I said I'm sure there's an easy way to do it but it escapes me..
    :
    : My out put packet currently looks like this:
    : CYPE>APRS,TCPIP*,qAC,APRSFL:@100232z5614.00N/11726.00W_160/[b]3[/b]g.
    : ..t057b10172
    :
    : It should look like this:
    : CYPE>APRS,TCPIP*,qAC,APRSFL:@100232z5614.00N/11726.00W_160/[b]003[/b]
    : g...t057b10172
    : Sean
    :

    You can format a number with leading zeros using [b]sprintf()[/b]
    e.g.

    $n = 3;
    $sn = sprintf("%03d", $n);

    Try [italic]perldoc -f sprintf[/italic] for more on this function
  • : : I'm sure there's a simple solution to my problem but not programing
    : : full time I'm not as learned as I'd like to be. I'm working on a
    : : script that grabs Airport weather data using Geo::METAR and formats
    : : it into a telemetry packet. Now a few of the fields are outputed
    : : from Geo::METAR in variable lengths because of changing weather
    : : conditions ie. 5 or 20 Mile per hour wind speeds. what I need to do
    : : is add leading zeros on so I have three bytes in the field once the
    : : script outputs the data ie 005 or 020.
    : : Like I said I'm sure there's an easy way to do it but it escapes me..
    : :
    : : My out put packet currently looks like this:
    : : CYPE>APRS,TCPIP*,qAC,APRSFL:@100232z5614.00N/11726.00W_160/[b]3[/b]g.
    : : ..t057b10172
    : :
    : : It should look like this:
    : : CYPE>APRS,TCPIP*,qAC,APRSFL:@100232z5614.00N/11726.00W_160/[b]003[/b]
    : : g...t057b10172
    : : Sean
    : :
    :
    : You can format a number with leading zeros using [b]sprintf()[/b]
    : e.g.
    :
    : $n = 3;
    : $sn = sprintf("%03d", $n);
    :
    : Try [italic]perldoc -f sprintf[/italic] for more on this function

    Thanks that did the trick.

    Sean

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories