I got this far with some code. It works fine except for some reason, this code requires a "blank line" between the define {....} records otherwise a record gets missed in the final array. I appear to be in some sort of "brain lock" right now! The solution has to be simple, and I've gotten some hints, but I'm not able to see it! Oh, yes I do know that my deleteDB() sub isn't the fastest critter on the block.
define host{ use windows-server .... } define host{ use windows-server } is ok, BUT define host{ use windows-server .... } define host{ use windows-server } is not ok!
#!usr/bin/perl -w use strict; use Data::Dumper; my @DB; my %hash = (); while (<DATA>) { if ( m/^define/.../}\s*$/) { next if /{\s*$/; next if /\s*\}\s*/; my ($hkey,$hval) = split; $hash{$hkey}=$hval; } else { push @DB, {%hash}; } } push @DB,{%hash}; print Dumper \@DB; #Array of Hash has been created deleteDB ('address' => '192.168.1.1'); #delete one record sub deleteDB { my ($key,$value) = @_; @DB = grep {$_->{"$key"} ne "$value"} @DB; } print "=============================\n"; print Dumper \@DB; #Prints: #$VAR1 = [ # { # 'use' => 'windows-server', # 'hostgroups' => '060202', # 'host_name' => 'serverA', # 'address' => '192.168.1.1', # 'alias' => '060202', # 'contact_groups' => 'yu' # }, # { # 'use' => 'windows-server', # 'hostgroups' => '060202', # 'host_name' => 'serverA', # 'address' => '192.168.1.11', # 'alias' => '060202', # 'contact_groups' => 'yu' # }, # { # 'use' => 'windows-server', # 'hostgroups' => '060202', # 'host_name' => 'serverA', # 'address' => '192.168.1.21', # 'alias' => '060202', # 'contact_groups' => 'yu' # } # ]; #============================= #$VAR1 = [ # { # 'use' => 'windows-server', # 'hostgroups' => '060202', # 'host_name' => 'serverA', # 'address' => '192.168.1.11', # 'alias' => '060202', # 'contact_groups' => 'yu' # }, # { # 'use' => 'windows-server', # 'hostgroups' => '060202', # 'host_name' => 'serverA', # 'address' => '192.168.1.21', # 'alias' => '060202', # 'contact_groups' => 'yu' # } # ]; __DATA__ define host{ use windows-server host_name serverA hostgroups 060202 alias 060202 contact_groups yu address 192.168.1.1 } define host{ use windows-server host_name serverA hostgroups 060202 alias 060202 contact_groups yu address 192.168.1.11 } define host{ use windows-server host_name serverA hostgroups 060202 alias 060202 contact_groups yu address 192.168.1.21 }
In reply to Re: how to remove matched multi lines
by Marshall
in thread how to remove matched multi lines
by cxfcxf
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |