Other specific solutions are great. But I got onto a different track as I think the idea of the range ... operator is a good one as this leads to a general solution that can be used to sort the input records by some arbitrary input criteria, etc. The idea being to make an Array of Hash and then process that...

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.