My idea for your problem looks like this:

#!/usr/bin/perl use strict; use warnings; my $datafile = 'account.data'; my $searched_customer = 'customer2'; # modifications of values my %modify = ( credit => -100, debit => 250, ); my %indent = ( bakjob => ' 'x2, type => ' 'x4, customer => ' 'x6, ); # my %re = ( bakjob => qr{^$indent{bakjob}bakjob(\d+)_details}, type => qr{^ (debit|credit) =}, ); #open my $fh, '<', $datafile or die $!; my $fh = *DATA; # don't start before this line my $line_num = 4; while ( my $line = <$fh> ) { # only parse complete bakjob blocks after line $line_num if ( $. > $line_num && $line =~ m/$re{bakjob}/ .. $line =~ m/^$indent{bakjob}},?/ ) { # parse internal structure if ( $line =~ m/$re{type}/ ) { print $line; parse( $fh, $1 ); } else { print $line; } } # maybe this is wanted, if you need to get all lines printed: # if ( $. < $line_num ) { # print $line; # elsif ( $line =~ m/$re{bakjob}/ .. $line =~ m/^$indent{bakjob}}, +?/ ) { # # parse internal structure # .... # } # else { # print $line; # } } sub parse { my ( $fh, $type ) = @_; while ( my $line = <$fh> ) { chomp $line; my ( $customer, $value ) = split m{\s*=\s*|,}, $line; $customer =~ s/^\s+//; if ( $customer eq $searched_customer ) { printf "$indent{customer}%s = %.1f\n", $customer, $value+$ +modify{$type}; } else { print $line, "\n"; } last if $line =~ m/^$indent{type}},?/; } } __DATA__ bakjob1_details = { credit = { customer1= 2000.0, customer2 = -1500.0 customer3 = 0.0, }, debit = { customer1= 50000.0, customer2 = -2000.0, customer3 = 0.0, } }, bakjob2_details = { credit = { customer1= 1000.0, customer2 = 200.0, customer3 = 500.0, }, debit = { customer1= 600.0, customer2 = 659.0, customer3 = 887.0, } }

updates:

  1. typo fixed; thanks kyle

In reply to Re: Pattern Matching - a Tricky one by linuxer
in thread Pattern Matching - a Tricky one by John007

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.