Re: Locating an element within an array
- From: "kens" <kenslaterpa@xxxxxxxxxxx>
- Date: 30 Oct 2006 12:52:25 -0800
deadpickle wrote:
I am trying to find the SLP within a raw metar. I will post the metar
and the program below. What I want to do is go through each element of
the array and check for certain values. The last part of the program is
giving me problems. It is suppost to find the element that starts with
SLP and return the last 3 integers. Instead of only finding the one
with SLP it is also returning the value of any 6 digit element. hope
this is clear, any ideas?
Source file:
-----------------------------------------------------------------------
KCHS 131656Z 15009KT 10SM -RA FEW027 BKN044 OVC065 24/20 A2994 RMK AO2
RAB44 SLP137 P0000 T02440200
Program:
-----------------------------------------------------------------------
Always start your program with:
use strict;
use warnings;
This will force you to define variable (for example:
my $file="KCHS.txt";
This will save you from spelling errors.
#loads metar file and sets its elements into an array
$file="KCHS.txt";
open (IN, $file) || die("Could Not Open File");
Get in the habit of using the 3 argument form of open
and print the error info ($!).
open my $inFH, "<", $file or die
"Could Not Open File: $!"
$metar=<IN>;
close(IN);
print("$metar\n");
@metar=split(" ", $metar);
#remove station name and time
shift(@metar);
shift(@metar);
#loop through all elements
for ($n=0; $n<20; $n++)
do you know there are 20 elements?
Why not use the foreach loop:
foreach (@metar)
{
#Test the elements to find the wind spd and dir
if (length $metar[$n] == 7)
With the foreach loop this becomes:
if (length($_) == 7)
{same as above regarding open.
open(OUT, ">>metar.txt") || die("could Not Open File!");
#print wind dir to file
print OUT (substr($metar[$n],0,3),"\n");
#print wind spd to file
print OUT (substr($metar[$n],3,2),"\n");
}
#Test the elements for temp
if (length $metar[$n] == 5)
{
#divide into T and Td
($t,$td)=split("/",$metar[$n]);
#make sure it is the correct T and Td
if ((length($t) && length($td)) == 2)
Note that you are only checking that the length of $td
is 2. As long as length of $t does not return false (e.g. 0),
it passes the test.
{
#convert to F and print to file
$tf=($t*(9/5))+32;
$ntf=sprintf("%.0f",$tf);
$tdf=($td*(9/5))+32;
$ntdf=sprintf("%.0f",$tdf);
print OUT ("$ntf\n");
print OUT ("$ntdf\n");
}
}
#Test for SLP
if (length $metar[$n] == 6)
{
print ($metar[$n], "\n");
if (substr($metar[$n],0,3) == "SLP")
The problem with this line is you are using a numeric comparison
instead of string comparison (eq).
{
print (substr($metar[$n],3,3), "\n");
}
}
};
*I removed all of the test "print" lines so that it is easier to
understand*
HTH.From a quick look at the code those are the comments I came up with.
Ken
.
- References:
- Locating an element within an array
- From: deadpickle
- Locating an element within an array
- Prev by Date: what's wrong with this code?
- Next by Date: POE performance issue.
- Previous by thread: Locating an element within an array
- Next by thread: what's wrong with this code?
- Index(es):
Relevant Pages
|