Perl

Moderators: Jonathan
Number of threads: 1236
Number of posts: 3605

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Adding leading zeros to variable data Posted by ve6sar on 9 Aug 2007 at 7:44 PM
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/3g...t057b10172

It should look like this:
CYPE>APRS,TCPIP*,qAC,APRSFL:@100232z5614.00N/11726.00W_160/003g...t057b10172
Sean
Report
Re: Adding leading zeros to variable data Posted by ndixon on 16 Aug 2007 at 2:11 AM
: 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/3g.
: ..t057b10172
:
: It should look like this:
: CYPE>APRS,TCPIP*,qAC,APRSFL:@100232z5614.00N/11726.00W_160/003
: g...t057b10172
: Sean
:

You can format a number with leading zeros using sprintf()
e.g.

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

Try perldoc -f sprintf for more on this function
Report
Re: Adding leading zeros to variable data Posted by ve6sar on 16 Aug 2007 at 6:38 AM
: : 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/3g.
: : ..t057b10172
: :
: : It should look like this:
: : CYPE>APRS,TCPIP*,qAC,APRSFL:@100232z5614.00N/11726.00W_160/003
: : g...t057b10172
: : Sean
: :
:
: You can format a number with leading zeros using sprintf()
: e.g.
:
: $n = 3;
: $sn = sprintf("%03d", $n);
:
: Try perldoc -f sprintf for more on this function

Thanks that did the trick.

Sean




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.