This code will parse the whole file into a hash. Then you can manipulate it via the supplied subroutines (add_keyword, del_keyword, get_keyword). You can find examples to their usages to the end of the code.

#!/usr/bin/perl -w use strict; use Data::Dumper; sub read_config { my $file = shift; # Slurp in the file open (RUNSET, "<$file") or die "Can't access '$file': $!"; my %conf; my $handle; while (<RUNSET>) { chomp; next if $_ eq '' or /^;/; if (/^\*(.*)$/) { if ($1 eq 'END') { undef($handle); } else { $handle = $1; %{$conf{$handle}} = (); } next; } if ($handle) { my($var, $value) = split('\s*=\s*', $_, 2); next unless defined $var; $conf{$handle}{$var} = $value; } } close RUNSET; return \%conf; } sub check_params { my ($config, $block, $keyword, $value) = @_; return unless ref($config) eq 'HASH'; return unless defined($block); return unless ref($config->{$block}) eq 'HASH'; return unless defined($keyword); return @_; } sub add_keyword { my ($config, $block, $keyword, $value) = check_params(@_); return unless defined($config); if (defined(${$config->{$block}}{$keyword})) { print "Replacing value of $keyword in $block (previously " . ${$config->{$block}}{$keyword} . ").\n"; } else { print "Adding the new $keyword keyword to $block block.\n"; } ${$config->{$block}}{$keyword} = $value; } sub del_keyword { my ($config, $block, $keyword) = check_params(@_); return unless defined($config); if (defined(${$config->{$block}}{$keyword})) { print "Deleting $keyword from $block.\n"; return delete(${$config->{$block}}{$keyword}); } else { print "Tried to delete a nonexistant key $keyword in $block block. +\n"; } } sub get_keyword { my ($config, $block, $keyword) = check_params(@_); return unless defined($config); if (defined(${$config->{$block}}{$keyword})) { return ${$config->{$block}}{$keyword}; } else { print "Tried to access a nonexistant key $keyword in $block block. +\n"; } } my $config = read_config('test.txt'); add_keyword($config, 'DESCRIPTION', 'test', 10); add_keyword($config, 'DESCRIPTION', 'keyword', 'aa'); del_keyword($config, 'DESCRIPTION', 'keyword2'); del_keyword($config, 'DESCRIPTION', 'keyword3'); print Dumper($config); exit;

I hope this helps.

--
Alper Ersoy

In reply to Re: Search for a BLOCK of text and selectively replace by aersoy
in thread Search for a BLOCK of text and selectively replace by Rhodium

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.