[Cialug] Perl regex

Chris Freeman cwfreeman at gmail.com
Tue Jun 19 14:01:03 CDT 2007


On 6/19/07, Daniel A. Ramaley <daniel.ramaley at drake.edu> wrote:
>
> On Tuesday 19 June 2007 11:11, Josh More wrote:
> >Instead of slurping (bad habit, btw), read line by line in a while.
> >Then, split each line on the =, and assign to a hash with the
> >concatenate operator.
>
> Thanks for the response! I guess i should have provided more detail as
> to my problem specification. Your sample code was very similar to what
> i'm trying to replace.
>
> The files in question are guaranteed to only be a few kB in size, so
> slurping them is not a problem. I have a few reasons for wanting to
> process the file as one big string rather than break it into a hash and
> reconstituting the file later:
> 1) Character set detection/conversion (input files are one of ASCII,
>    ISO-8859-1, or UTF-8; i'm converting to UTF-8 as necessary).
> 2) Comments should be preserved.
> 3) Original line ordering should be preserved.



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).

You will have to specify how to deal with:

A=bob
# Comment in the middle...
A=jones

and:

A=bob
B=jr
A=jones

But, presuming the 'obvious' solution, it should be simply:

my @lines;
my %keys;
while(<FILE>) {

    my $line = $_;
    chomp $line;

    if( $line =~ /^([^#=])+=(.*)$/ ) {
        if( !defined($keys{$1) ) {
            push(@lines, \$1);
            $keys{$1} = $#lines;
        } else {
            $lines[$keys{$1}] .= $2;
        }
    } else {
        push(@lines,$line);
    }
}

for my $line (@lines) {
    print $line . "\n";
}

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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://cialug.org/pipermail/cialug/attachments/20070619/a47fc051/attachment.htm


More information about the Cialug mailing list