Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

(Kozz) Parse into hash of hashrefs

by Kozz (Friar)
on Sep 24, 2002 at 02:57 UTC ( #200281=note: print w/ replies, xml ) Need Help??

in reply to Positional Pattern-Matching

Perhaps some monks will decrie some inefficiencies in my code, but I think this works fairly well and has been tested:

#!/usr/local/bin/perl -w use strict; use POSIX; # added after observing BrowserUK's code my (%schedule, $backup_type); my $today = strftime( '%A', localtime ); # changed after observing Bro +wserUK's code while (my $line = <DATA>) { chomp $line; my $count = 0 ; if ($line =~ /schedule:\s+(\w+)/i){ my $count++; $backup_type = $1; } if ( $line =~ /(\w+day)\s+(\d{2}\:\d{2}\:\d{2})/i ){ $schedule{$1} = { 'backup_type' => $backup_type, 'time' => $2 }; } } printf "Today is %s and the backup type is %s to begin at %s\n", $today, $schedule{$today}->{'backup_type'}, $schedule{$today}->{'tim +e'}; __DATA__ ...etc
So you're making a hash of hashrefs - the key of the parent hash is the day of the week, and then "type" and "time" are accessible through each day of the week.

Any improvements are welcomed. I may be playing fast and loose with the regex to match the day of the week, depending on what else is in your __DATA__.


edit: removed unneeded /g modifier from regexes, modified code to get current DOW as seen in BrowserUK's code (I learn something every day)

Comment on (Kozz) Parse into hash of hashrefs
Download Code

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2015-10-06 20:40 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (160 votes), past polls