drose2211 has asked for the wisdom of the Perl Monks concerning the following question:
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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: substitution with regex
by 1nickt (Canon) on Jan 19, 2018 at 02:43 UTC | |
by drose2211 (Sexton) on Jan 23, 2018 at 03:15 UTC | |
by Athanasius (Archbishop) on Jan 23, 2018 at 06:46 UTC | |
by AnomalousMonk (Archbishop) on Jan 23, 2018 at 12:15 UTC | |
|
Re: substitution with regex
by Laurent_R (Canon) on Jan 19, 2018 at 07:30 UTC | |
by soonix (Chancellor) on Jan 19, 2018 at 12:46 UTC | |
by Laurent_R (Canon) on Jan 19, 2018 at 17:52 UTC | |
|
Re: substitution with regex
by Anonymous Monk on Jan 19, 2018 at 01:12 UTC |