Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

(Kozz) Parse into hash of hashrefs

by Kozz (Friar)
on Sep 24, 2002 at 02:57 UTC ( [id://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__.

--Kozz

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-03-28 23:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found