Locating an element within an array
- From: "deadpickle" <deadpickle@xxxxxxxxx>
- Date: 30 Oct 2006 12:20:47 -0800
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:
-----------------------------------------------------------------------
#loads metar file and sets its elements into an array
$file="KCHS.txt";
open (IN, $file) || 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++)
{
#Test the elements to find the wind spd and dir
if (length $metar[$n] == 7)
{
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)
{
#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")
{
print (substr($metar[$n],3,3), "\n");
}
}
};
*I removed all of the test "print" lines so that it is easier to
understand*
.
- Follow-Ups:
- Re: Locating an element within an array
- From: kens
- Re: Locating an element within an array
- Prev by Date: Re: Opening .dat file in perl
- Next by Date: what's wrong with this code?
- Previous by thread: Simple calendar
- Next by thread: Re: Locating an element within an array
- Index(es):
Relevant Pages
|