Thank you very much for your tips. With my irregual data I had to change my code( and data) and your tips helped alot.
In my check table (csv), I have 3 character codes(ICAO) and their equivalent 2 character codes(IATA) and descriptions. At first, IATA was at first column so I needed previous element of the array when the key was found. But after having an example of irregular ICAO code, which has 2 characters that matches its IATA code, the code didn't help since Perl stops after the first matching. And that IATA code is used three times in the table ( 1 ICAO, 2 IATA)
Then I changed IATA and ICAO columns' order and changed the code with your help and now it works. Though it may be clumsy, i think its pretty clear.
--$hash{'AirIATA'}= get_IATA($airlineLoc,$hash{'Airline'}) ;
-------------------------------------------------------
sub get_IATA
{
my $file = shift;
my $airline = shift;
my @data;
open(my $fh, '<', $file) or die "Can't read file '$file' [$!]\
+n";
while (my $line = <$fh>)
{
my @fields = split(/;/, $line);
push @data, @fields;
}
close($fh);
my @results= map
{
$data[$_] =~ m{ \A \Q$airline\E }xms ? $data[$
+_ + 1] : ()
}
1 .. $#data;
if ($#results>1)
{
return $results[1];
}
elsif ($#results>0) # Do I need a second check, like <2 ?
{
return $results[0];
}
else
{
print "Airline $airline has no definition in $file fil
+e\n";
exit; # Program should stop if there is no match
}
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|