thundergnat has pointed out the problem with the accidental globbing. I think you can make the code simpler and easier to read if you dispense with the intermediate
@dir_list variable because you can autovivify each array directly. I have used
while (<DATA>) {...} to read into
$_ so that matches and the chomp are less cluttered. I think I'm right in saying (and I'm sure I'll be corrected if I'm wrong) that the
foreach will also read the entire file in order to build a list that it can iterate over; possibly not what you want.
I have left the chomp until after rejecting the comment lines; no point in doing work on something we're going to throw away. You don't need to escape the equals in the match and I'm not sure why you were capturing the equals signs either side of the server name; perhaps there was a reason that has been lost as you reduced the code for your post.
use strict;
use warnings;
use Data::Dumper;
my $rec = q{};
my %HoA = ();
while (<DATA>)
{
next if m{^#};
chomp;
if (m{^==(\w+)==})
{
$rec = $1;
}
else
{
push @{$HoA{$rec}}, $_;
}
}
print Data::Dumper->Dump([\%HoA], ['*HoA']);
print qq{\n};
foreach my $server (keys %HoA)
{
foreach my $unc (@{$HoA{$server}})
{
print qq{$server: $unc\n};
}
}
__DATA__
########## Server 1 UNC's ########################
==SERVER1==
\\server1\share1\tld0\sub-dir1\sub-dir2
\\server1\share1\tld0\sub-dir2\sub-dir2
\\server1\share2\tld1\dir1\subdir2\subdir3
\\server1\share2\tld1\dir1\subdir2\subdir4
########## Other Server UNC's ########################
==REMOTESERVER1==
\\remoteserver1\share1\tld1\dir1\subdir2\subdir4
==REMOTESERVER2==
\\remoteserver2\share1\tld1\dir1\sub dir 3\sub dir 5\report's
\\remoteserver2\share1\tld1\dir2\sub dir 4\sub dir 6\reports
###### No Entries Below This Line ######
Here's the output. Note that Data::Dumper does it's own escaping when serializing the data.
%HoA = (
'REMOTESERVER1' => [
'\\\\remoteserver1\\share1\\tld1\\dir1\\
+subdir2\\subdir4'
],
'SERVER1' => [
'\\\\server1\\share1\\tld0\\sub-dir1\\sub-dir2
+',
'\\\\server1\\share1\\tld0\\sub-dir2\\sub-dir2
+',
'\\\\server1\\share2\\tld1\\dir1\\subdir2\\sub
+dir3',
'\\\\server1\\share2\\tld1\\dir1\\subdir2\\sub
+dir4'
],
'REMOTESERVER2' => [
'\\\\remoteserver2\\share1\\tld1\\dir1\\
+sub dir 3\\sub dir 5\\report\'s',
'\\\\remoteserver2\\share1\\tld1\\dir2\\
+sub dir 4\\sub dir 6\\reports'
]
);
REMOTESERVER1: \\remoteserver1\share1\tld1\dir1\subdir2\subdir4
SERVER1: \\server1\share1\tld0\sub-dir1\sub-dir2
SERVER1: \\server1\share1\tld0\sub-dir2\sub-dir2
SERVER1: \\server1\share2\tld1\dir1\subdir2\subdir3
SERVER1: \\server1\share2\tld1\dir1\subdir2\subdir4
REMOTESERVER2: \\remoteserver2\share1\tld1\dir1\sub dir 3\sub dir 5\re
+port's
REMOTESERVER2: \\remoteserver2\share1\tld1\dir2\sub dir 4\sub dir 6\re
+ports
I hope this is of use.
Cheers,
JohnGG
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.