Re: Locating an element within an array




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)

{
open(OUT, ">>metar.txt") || die("could Not Open File!");
same as above regarding open.
#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*

From a quick look at the code those are the comments I came up with.
HTH.
Ken

.



Relevant Pages

  • Re: foreach statement output
    ... by using a 'for' statement instead of the foreach loop: ... I thought you wanted a string. ... Why are you declaring an array instead? ... Eliminate duplicate strings completely? ...
    (comp.infosystems.www.authoring.cgi)
  • Re: Input error checking on arrays..
    ... >> It would be ideal if instead of even going with a foreach loop, ... > to check every member of the array for certain characteristics, ... > And you can't check every member of an array without looping through the ... input error output file I've got (in which I indicate what the error ...
    (comp.lang.php)
  • html template and tables
    ... Print out the table from an array using HTML teplates. ... } # End the foreach loop here. ... template section ...
    (perl.beginners)
  • Re: Problem with Range Operator sequence number
    ... > file and adds any found in the range to an array. ... > This piece of code is part of a foreach loop which iterates two times. ... This means that the range operator is still true. ... brackets or else, simply backslah the brackets, '\', because ...
    (comp.lang.perl)
  • Re: Problem with Range Operator sequence number
    ... > file and adds any found in the range to an array. ... > This piece of code is part of a foreach loop which iterates two times. ... This means that the range operator is still true. ... brackets or else, simply backslah the brackets, '\', because ...
    (comp.lang.perl.misc)