Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Using a Hash Variable in an If Statement

by toolic (Bishop)
on May 11, 2015 at 19:32 UTC ( [id://1126337]=note: print w/replies, xml ) Need Help??


in reply to Using a Hash Variable in an If Statement

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

Replies are listed 'Best First'.
Re^2: Using a Hash Variable in an If Statement
by Bama_Perl (Acolyte) on May 11, 2015 at 20:21 UTC
    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 }
        Thank you for the response, edimusrex. That makes a lot of sense now!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1126337]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-26 02:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found