A fuzzy approach...

Advantage: may handle large files (no slurping). 'Regexp's have to handle a single line only.
Disadvantage: bakjob*_details, credit/debit, customer2 have to appear on distinct lines (fragile, relies on proper input).

use strict; my %replacement = ("bakjob1_details.debit" => 123.45, "bakjob2_details.credit" => -543.21 ); sub new_value { my ($key, $value) = @_; if (defined $replacement{$key}) { $value = $replacement{$key}; } else { warn "WARNING: '$key' unmodified ($value)\n"; } return $value; } my ($bjob, $payment); while ( <DATA> ) { $bjob = $1, next if /\b(bakjob[12]_details)\b/; $payment = $1, next if /\b(credit|debit)\b/; # next unless /\bcustomer2\b/; #speed-up? s{^ (\s* customer2 \s* = \s*) (\S+?) (\s*?,?\s*) $ } { $1 . new_value("$bjob.$payment",$2) . $3 }ex; } continue { print; } __DATA__ bakjob1_details = { credit = { customer1= 2000.0, customer2 = -1500.0 # comma missing? 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, } }

In reply to Re: Pattern Matching - a Tricky one by Perlbotics
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.