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:

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);
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:
1 time1 time2 ... time_n 2 time1 time2 ... time_n.
Any help would be appreciated. Thanks!

In reply to Using a Hash Variable in an If Statement by Bama_Perl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.