Lotus1 has asked for the wisdom of the Perl Monks concerning the following question:
I got this program to work but it seems like there could be a better way to do this. I have a text file with server host names followed by optional comments. Any suggestions?
use warnings; use strict; use Data::Dumper; my %hosts; #### file format is hostname space optional #'s followed by comment while(<DATA>) { chomp; my $host; my $comment; if(/^\s*(\w+)\s*#+(.*$)/){ $host = $1; $comment = $2; } elsif(/^\s*(\w+)\s*$/){ $host = $1; $comment = ''; } $hosts{$host} = $comment; } print Dumper(\%hosts); __DATA__ XYZ1ADAIQ1 #AMI XYZ1ADECQ1 #OAG XYZ1ADEDQ1 XYZ1ADEDQ2 ##DMS Host
The output is:
$VAR1 = { 'XYZ1ADEDQ1' => '', 'XYZ1ADECQ1' => 'OAG', 'XYZ1ADEDQ2' => 'DMS Host', 'XYZ1ADAIQ1' => 'AMI' };
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: capture optional text
by tybalt89 (Monsignor) on May 17, 2017 at 22:12 UTC | |
by Lotus1 (Vicar) on May 18, 2017 at 14:40 UTC | |
by KurtZ (Friar) on May 18, 2017 at 16:23 UTC | |
by tybalt89 (Monsignor) on May 18, 2017 at 16:33 UTC | |
Re: capture optional text
by haukex (Archbishop) on May 18, 2017 at 04:56 UTC | |
Re: capture optional text
by Anonymous Monk on May 17, 2017 at 22:03 UTC | |
by haukex (Archbishop) on May 18, 2017 at 13:55 UTC | |
by Lotus1 (Vicar) on May 18, 2017 at 13:32 UTC |