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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |