in reply to Generate Hash of hashes by reading a large Input file

I'd create a state machine to parse the file. The state ($section in the below code) tells me what the parser expects to find. I also need to store the current zone and cluster to be able to attach the new information to the correct part of the structure.
#!/usr/bin/perl use warnings; use strict; my %zone; my $section = 0; my ($current_zone, $current_cluster); while (<>) { if (! $section) { if (/^Zone ID/) { $section = 'zones'; } elsif (/^Cluster ID/) { $section = 'clusters'; } elsif (/^Host Name/) { $section = 'hosts'; } elsif (my ($name) = /^(\S+)$/) { if (exists $zone{$name}) { $current_zone = $name; } elsif (exists $zone{$current_zone}{cluster}{$name}) { $current_cluster = $name; } } } elsif ('zones' eq $section) { if (my ($id, $name) = /(\S+)\s+(\S+)/) { $zone{$name}{id} = $id; } elsif (/^$/) { $section = 0; } } elsif ('clusters' eq $section) { if (my ($id, $name, $type, $mem, $cpu) = /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)$/ ) { $zone{$current_zone}{cluster}{$name} = { id => $id, type => $type, mem => $mem, cpu => $cpu }; } elsif (/^$/) { $section = 0; } } elsif ('hosts' eq $section) { if (my ($host, $vms, $status, $state) = /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)$/ ) { $zone{$current_zone}{cluster}{$current_cluster}{host}{$hos +t} = { vms => $vms, status => $status, state => $state }; } elsif (/^$/) { $section = 0; } } } use Data::Dumper; print Dumper \%zone;
($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^2: Generate Hash of hashes by reading a large Input file
by pr33 (Scribe) on Apr 20, 2017 at 20:05 UTC
    Thank you Choroba . I tried your code and execution time has improved a lot upon parsing multiple files of the same input format.

    I was more interested in the Total CPU/Memory/Storage allocation than the Overcommit ratio, So added one more section to the code .

    .... some lines .... ....... } elsif(/^Resource Type/) { $section = 'resources'; ...... ..... elsif ('resources' eq $section) { if (my ($resource, $usage) = /^(CPU:|MEMORY:|ALLOCATED STORAGE:)\s+\S+\s+\S+\s+\S ++\s+\S+\s+\S+\s+\S+\s+(\S+)$/ ) { $usage =~ s/%//g; $zone{$current_zone}{cluster}{$current_cluster}{$resou +rce} = { usage => $usage}; } elsif (/^$/) { $section = 0; } ......