<br><br><div><span class="gmail_quote">On 6/19/07, <b class="gmail_sendername">Daniel A. Ramaley</b> &lt;<a href="mailto:daniel.ramaley@drake.edu">daniel.ramaley@drake.edu</a>&gt; 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>&gt;Instead of slurping (bad habit, btw), read line by line in a while.<br>&gt;Then, split each line on the =, and assign to a hash with the<br>&gt;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&#39;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>&nbsp;&nbsp; ISO-8859-1, or UTF-8; i&#39;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&#39;s suggestion to preserve line order and comments. I&#39;m not sure how UTF-8 conversion works better in a &#39;slurp&#39; than a line-by-line load, but the online docs suggest that you can convert pretty easily (possibly replace &#39;while()&#39; with a &#39;for()&#39; 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 &#39;obvious&#39; solution, it should be simply:
<br><br>my @lines;<br>my %keys;<br>while(&lt;FILE&gt;) {<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; my $line = $_;<br>&nbsp;&nbsp;&nbsp; chomp $line;<br><br>&nbsp;&nbsp;&nbsp; if( $line =~ /^([^#=])+=(.*)$/ ) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if( !defined($keys{$1) ) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; push(@lines, \$1);
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $keys{$1} = $#lines;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $lines[$keys{$1}] .= $2;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; } else {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; push(@lines,$line);<br>&nbsp;&nbsp;&nbsp; }<br>}<br></div><br>for my $line (@lines) {<br>&nbsp;&nbsp;&nbsp; print $line . &quot;\n&quot;;
<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>