Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: Using a Hash Variable in an If Statement

by Bama_Perl (Acolyte)
on May 11, 2015 at 20:21 UTC ( [id://1126347]=note: print w/replies, xml ) Need Help??


in reply to Re: Using a Hash Variable in an If Statement
in thread Using a Hash Variable in an If Statement

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.

Replies are listed 'Best First'.
Re^3: Using a Hash Variable in an If Statement
by edimusrex (Monk) on May 11, 2015 at 20:32 UTC
    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://1126347]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-26 00:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found