Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Using a Hash Variable in an If Statement

by Bama_Perl (Acolyte)
on May 11, 2015 at 19:20 UTC ( #1126335=perlquestion: print w/replies, xml ) Need Help??

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:

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!

Replies are listed 'Best First'.
Re: Using a Hash Variable in an If Statement
by toolic (Bishop) on May 11, 2015 at 19:32 UTC
    use warnings; use strict; my @names = qw( DONT MICH LEON RAPH SPLN SHRD CASY APRL FOOT BEBP RKST DUBY SAMH GRAW KNYN KP01 KP02 KP03 KP04 KP05 ); my %data; while (<DATA>) { next if /mcp/; chomp; my ($s, $n) = split; push @{ $data{$s} }, $n; } my $i = 1; for my $name (@names) { print "$i\n"; print "$_\n" for @{ $data{$name} }; $i++; } __DATA__ 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

    Prints:

    1 7.3196 -0.1453 2 7.5579 -1.5737 3 7.4596 -0.6237 4 7.0563 -1.3287 5 6.1113 -2.2053 6 6.2829 -2.4087 7 7.3879 -1.0153 8 7.1963 -2.1870 9 7.1496 -2.1903 10 7.1979 -2.3270 11 6.6879 -2.5337 12 6.3729 -1.9920 13 6.9529 -1.9653 14 7.0046 -1.5937 15 6.7313 -2.0403 16 17 18 19 20
      Could you possible explain the first while loop:
      my %data; while (<DATA>) { next if /mcp/; chomp; my ($s, $n) = split; push @{ $data{$s} }, $n; }
      If my file I'm trying to read in is called rat_event, should I open it above, such as here:
      open(rat_event,"RAT_EVENT"); @rat_event = <rat_event>;
      and then do:
      my %rat_event; while <RAT_EVENT> { # Same as above }
      Thanks for the help.
        The first part is saying "check each line of the file (assigned to DATA, in your case rat_event) and if the line is equal to mcp, skip it (would skip 20131201.06372602.mcp and 20131202.02185602.mcp). Else chomp the line (remove new line and carriage return) and split the line into 2 variables (s and n, since no character is specified for the split it defaults to a space which would give you the station name and the time) and once it has those variables assigned it puts the values into the hash %data so s => 'n'.

        Yes you should load the file first as your code is shown

        open my $fh, "<rat_event" or die "Failed to open:$!"; while(<$fh>) { #do something }
Re: Using a Hash Variable in an If Statement
by edimusrex (Monk) on May 11, 2015 at 20:14 UTC
    Adding to toolic's code if you wanted the station name plus the run number added to the output you could do the following

    #!/usr/bin/perl use warnings; #use strict; my @names = qw( DONT MICH LEON RAPH SPLN SHRD CASY APRL FOOT BEBP RKST DUBY SAMH GRAW KNYN KP01 KP02 KP03 KP04 KP05 ); my %data; open my $file, "<", "rat_event.txt" or die "Can't open file:$!"; while(<$file>) { next if /\.mcp/; chomp; my ($s, $n) = split; push @{ $data{$s} }, $n; } my $i = 1; for my $name (@names) { print "$name\n"; my $x = 1; for (@{$data{$name}}) { print "Time $x : $_\n"; $x++; } print "\n"; $i++; } close($file);
      I apologize for the many questions, but what does the  print "$_\n" for @{ $data{$name} }; do? Is this just saying that I want to print a new line for each piece of data within each $name? Thanks.
        it means print each value for the hash referenced at $data{$name} (which would look like this in a data dump (data{APRL} => 7.1963). $_ is a special variable which references the current element. This is the same thing

        foreach my $line (@{$data{$name}}) { #do something }


        Where $line is the same thing as $_, more than 1 way to skin a cat. So it's say print out the hash value of the specified key and also print a new line with it so everything won't be bunched up on a single line. Hope that makes sense.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1126335]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (2)
As of 2021-05-15 09:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (150 votes). Check out past polls.

    Notices?