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' };
In reply to capture optional text by Lotus1
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |