Oddly, this does not sound like homework (;^). Okay, since the source data is exactly like what you show (though I presume you want to handle lots of cases with different keys and values in the hash, but hewing to the same rigid syntax), this ought to work:
use strict; $/ = undef; $_ = <DATA>; my ($prefix,$objdef,$suffix) = ( /(.*?\$obj = \{)(.*?)(\}.*)/s ); my @hashdef = ( $objdef =~ /\s*([A-Z_]+)\s*=>\s*(\'.*?\')/g ); print $prefix; for ( my $i=0; $i < scalar @hashdef; $i+=2 ) { print "\n\"$hashdef[$i]\"=>$hashdef[$i+1]"; } print "\n$suffix"; __DATA__ package Stats; sub new { my $pkg = shift; my $obj = { TOTAL_REPLIES => '6373', TOTAL_TOPICS => '574', TOTAL_MEMBERS => '211', LAST_REG_MEMBER_ID => '85-1039338924', LAST_REG_MEMBER_N => 'Unlimited_Destroyer', M_ONLINE_COUNT => '25', M_ONLINE_DATE => '1037453854', }; bless $obj, $pkg; return $obj; } 1; ; } 1;
I didn't try counting the cpu load on this, but it should be negligeable.

Your description made it sound like the stuff outside the curlies for the "$obj" assignment should be left unaltered, so the first regex match/assignment splits the whole thing into the initial, middle and final pieces, and the second regex match/assignment locates all the strings to be maintained from within the middle piece (putting these in a plain array to preserve order, in case that matters). Then print the first piece, loop over the sub-parts of the middle piece to print without those annoying spaces, and finish by printing the third piece.

update:oops, forgot that you wanted double quotes around the hash keys in the print-out. Also, if you don't want to print the "$prefix" and "$suffix" parts, just drop those two print statements, and put the "\n" at the end of the print statement in the for loop.


In reply to Re: Data handling by graff
in thread Data handling by Kage

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.