Hey dude, not everywhere has perfect climate like Santa Monica! As you may know, negative temps exist at some locations (and are denoted with a leading 'M' in METAR reports).

Here is the skeleton of a solution handling negative temperatures (using the reading from Fairbanks, AK at this time for testing).

(But see also Geo::METAR for an oldie-but-goodie parser for this format, which provides the temp in F in its dump output.)

use strict; use warnings; while ( my $line = <DATA> ) { $line =~ s{ \s (M?\d{2}) / (M?\d{2}) \s } { sprintf ' %d/%d ', convert($1), convert($2) }xeg; print $line; } sub convert { $_[0] =~ s/M/-/r * 9/5 + 32 }; __DATA__ KSMO 181551Z 00000KT 10SM CLR 14/08 A3009 RMK AO2 SLP189 T01440083 PAFA 190153Z 00000KT 5SM -SN BR FEW012 BKN025 OVC070 M07/M08 A2959 RMK + AO2 SLP030 T10671078
Output:
KSMO 181551Z 00000KT 10SM CLR 57/46 A3009 RMK AO2 SLP189 T01440083 PAFA 190153Z 00000KT 5SM -SN BR FEW012 BKN025 OVC070 19/17 A2959 RMK A +O2 SLP030 T10671078

Hope this helps!


The way forward always starts with a minimal test.

In reply to Re: substitution with regex by 1nickt
in thread substitution with regex by drose2211

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.