<br><br><div><span class="gmail_quote">On 6/19/07, <b class="gmail_sendername">Daniel A. Ramaley</b> <<a href="mailto:daniel.ramaley@drake.edu">daniel.ramaley@drake.edu</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On Tuesday 19 June 2007 11:11, Josh More wrote:<br>>Instead of slurping (bad habit, btw), read line by line in a while.<br>>Then, split each line on the =, and assign to a hash with the<br>>concatenate operator.<br>
<br>Thanks for the response! I guess i should have provided more detail as<br>to my problem specification. Your sample code was very similar to what<br>i'm trying to replace.<br><br>The files in question are guaranteed to only be a few kB in size, so
<br>slurping them is not a problem. I have a few reasons for wanting to<br>process the file as one big string rather than break it into a hash and<br>reconstituting the file later:<br>1) Character set detection/conversion (input files are one of ASCII,
<br> ISO-8859-1, or UTF-8; i'm converting to UTF-8 as necessary).<br>2) Comments should be preserved.<br>3) Original line ordering should be preserved.</blockquote><div><br><br>It should be pretty easy to adapt Josh's suggestion to preserve line order and comments. I'm not sure how UTF-8 conversion works better in a 'slurp' than a line-by-line load, but the online docs suggest that you can convert pretty easily (possibly replace 'while()' with a 'for()' across the array of lines in the file).
<br><br>You will have to specify how to deal with:<br><br>A=bob<br># Comment in the middle...<br>A=jones<br><br>and:<br><br>A=bob<br>B=jr<br>A=jones<br><br>But, presuming the 'obvious' solution, it should be simply:
<br><br>my @lines;<br>my %keys;<br>while(<FILE>) {<br> <br> my $line = $_;<br> chomp $line;<br><br> if( $line =~ /^([^#=])+=(.*)$/ ) {<br> if( !defined($keys{$1) ) {<br> push(@lines, \$1);
<br> $keys{$1} = $#lines;<br> } else {<br> $lines[$keys{$1}] .= $2;<br> }<br> } else {<br> push(@lines,$line);<br> }<br>}<br></div><br>for my $line (@lines) {<br> print $line . "\n";
<br>}<br><br>Obviously, it is longer. But no one (including yourself) will come back to visit you in six months/one year/three years with a Percussive Teaching Instrument.<br><br><br></div><br>