dayton has asked for the wisdom of the Perl Monks concerning the following question:
I know this is somewhat simple, but I've been unable to wrap my head around it. I have a large txt file in the format:
*******************Folder North*************** Folder : North Folder : Lab Host : host42016 VM : host42229 VM : host42235 VM : host42236 VM : host42237 VM : host42238 VM : host42239 VM : host42240 VM : host42241 VM : host42242 VM : host42252 Host : host42049 VM : host42361 VM : host42362 VM : host42363 VM : host42364 VM : host42365 VM : host42366 VM : host42367 VM : host42368 VM : host42369 VM : host42370 *******************Folder South*************** Folder : South Folder : Staging Host : host42124 VM : host42248 VM : host42249 VM : host42250 VM : host42230 VM : host42251 VM : host42243 VM : host42244 VM : host42245 VM : host42246 VM : host42247 Host : host42125 VM : host42299 VM : host42300 VM : host42301 VM : host42302 VM : host42303 VM : host42304 VM : host42305 VM : host42306 VM : host42307 VM : host42308
I want to read the file, and populate a hash with the "Host" as the key the various "VM" as it's values. Seems easy enough and I have that with:
open (FILE, 'data.txt') || die "Unable to open file"; while (<FILE>) { $line=$_; if ($line =~ /Folder/ || $line eq ""){ next; } ($type,$name)=split(/:/); $type =~ s/^\s+//; $type =~ s/\s+$//; $name =~ s/^\s+//; $name =~ s/\s+$//; if ($type=~"Host"){ $host=$name; }elsif ($type=~"VM"){ $guest=$name; push @{$hash{$host}}, $guest; } }
Where I'm running into a brick wall is:
- how do I remove duplicates (Host and VM)?
- how do I then count the # of values per key?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Parsing text file into a hash with multiple values per key
by Kenosis (Priest) on Oct 03, 2012 at 22:41 UTC | |
by dayton (Acolyte) on Oct 03, 2012 at 22:53 UTC | |
by Kenosis (Priest) on Oct 03, 2012 at 22:58 UTC | |
by Anonymous Monk on Oct 03, 2012 at 22:43 UTC | |
by Kenosis (Priest) on Oct 03, 2012 at 22:59 UTC | |
Re: Parsing text file into a hash with multiple values per key
by Anonymous Monk on Oct 03, 2012 at 22:40 UTC | |
Re: Parsing text file into a hash with multiple values per key
by sundialsvc4 (Abbot) on Oct 04, 2012 at 16:13 UTC | |
by dayton (Acolyte) on Oct 04, 2012 at 17:27 UTC |
Back to
Seekers of Perl Wisdom