Bama_Perl has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks, I have a question regarding the use of a hash. The goal of the following script is to loop through a file that looks like this:
20131201.06372602.mcp APRL 7.1963 BEBP 7.1979 CASY 7.3879 DONT 7.3196 DUBY 6.3729 FOOT 7.1496 GRAW 7.0046 KNYN 6.7313 LEON 7.4596 MICH 7.5579 RAPH 7.0563 RKST 6.6879 SAMH 6.9529 SHRD 6.2829 SPLN 6.1113 20131202.02185602.mcp APRL -2.1870 BEBP -2.3270 CASY -1.0153 DONT -0.1453 DUBY -1.9920 FOOT -2.1903 GRAW -1.5937 KNYN -2.0403 LEON -0.6237 MICH -1.5737 RAPH -1.3287 RKST -2.5337 SAMH -1.9653 SHRD -2.4087 SPLN -2.2053
Which is repeated >100 times. I am using a hash, because I first want to assign each string (DONT, MICH, etc) a number, which I have done so here in this hash:
my %station_name = ( DONT => "1", MICH => "2", LEON => "3", RAPH => "4", SPLN => "5", SHRD => "6", CASY => "7", APRL => "8", FOOT => "9", BEBP => "10", RKST => "11", DUBY => "12", SAMH => "13", GRAW => "14", KNYN => "15", KP01 => "16", KP02 => "17", KP03 => "18", KP04 => "19", KP05 => "20", );
What I want to do is loop through the above file (which is the format above), and for each station (DONT, MICH, etc), print out the respective number (time). However, I am unsure how to do this in an if-statement. What I have is below:
where rat_event is the file I'm looping through, $station and $delaytime are the values from above, but when it comes to the 'if' statement, I'm unsure how to use the hash to output something like:for ($i = 0; $i < @rat_event; $i++) { chomp($rat_event[$i]); ($station, $delaytime) = (split /\s+/, $rat_event[$i])[0,1]; # create a Hash for the Stations. if ($station = %station_name) { #I don't know what do do here. } close(rat_event); close(RAT_STA);
Any help would be appreciated. Thanks!1 time1 time2 ... time_n 2 time1 time2 ... time_n.
|
|---|