I am attempting to use regex on a text file that will match two numbers that represent temperature in Celsius. I attempted to put the numbers in two separate capture groups and then put them inside a formula to convert to Fahrenheit. I would then set two variables equal to two separate formulas with each using one of the capture groups. The point of this is to take a line like this:

KSMO 181551Z 00000KT 10SM CLR 14/08 A3009 RMK AO2 SLP189 T01440083

and take the "14/08" to convert it to Fahrenheit and substitute it back into the line. When I run my code I receive these errors:

Odd number of elements in anonymous hash at lab2-2.pl line 11.

Use of uninitialized value $1 in anonymous hash ({}) at lab2-2.pl line 11.

Odd number of elements in anonymous hash at lab2-2.pl line 12.

Use of uninitialized value $2 in anonymous hash ({}) at lab2-2.pl line 12.

readline() on closed filehandle IN at lab2-2.pl line 13.

I am wondering what I am missing. Is it an issue with my regex or somewhere else? Any help would be appreciated.

#usr/bin/perl use strict; use warnings; open IN,'<',"part2.txt"; open OUT,'>',"airport_fahrenheit.txt"; my $x; my $fahrenheit1 = (9 *{$1}/5) + 32; my $fahrenheit2 = (9 *{$2}/5) + 32; foreach $x (<IN>){ ( $x =~ s/(\d\d)\/(\d\d)/$fahrenheit1\/$fahrenheit2/g ); print OUT "$x"; } close IN; close OUT;

In reply to 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.