[Cialug] Perl regex
Josh More
morej at alliancetechnologies.net
Tue Jun 19 11:11:45 CDT 2007
You code will work better and be easier to maintain if you approach the
problem differently.
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.
---- example ----
#!/usr/bin/perl -w
use strict;
my %hash = ();
open(FILE,$ARGV[0]) || die "Could not open file $ARGV[0] - $!";
while(<FILE>) {
if ($_ !~ /^#/) {
chomp($_);
my ($k,$v) = split(/=/);
$hash{$k} .= $v;
}
}
foreach (sort(keys(%hash))) {
print "$_=${hash{$_}}\n";
}
---- end example ----
This way, you can give your code to someone else, and you won't have to
maintain it for the rest of your life.
-Josh More, RHCE, CISSP, NCLP, GIAC
morej at alliancetechnologies.net
515-245-7701
>>> "Daniel A. Ramaley" <daniel.ramaley at DRAKE.EDU> 06/19/07 10:57 AM
>>>
Since we're posting brain teasers, here's what i'm having trouble
with:
Is there a way to design a Perl s/// regex such that after each
replacement it starts searcing for the next replacement at the
beginning of where it found the previous match instead of after the
previous match?
What i'm trying to accomplish is to take files that look something like
this:
# Comment
AB=some text
CDE=text
CDE= more text
CDE=even more
F=foo
GH=bar,
GH= baz
And concatenate the continued lines together, so after processing it
looks like this:
# Comment
AB=some text
CDE=text more texteven more
F=foo
GH=bar, baz
Here's what i have so far, with the entire file contained in $data (i
already had to slurp the whole thing for other processing that was done
to it first):
1 while $data =~ s/^(([^#][^=]*)=.*)\n\2=/$1/gmo;
That works, but if there is a way to tell the regex parser to not
advance its internal pointer after each match, then i can get rid of
the useless while loop. (As it is now, without the while it only works
on lines that have been split into 2 (such as "GH" in the example) but
not lines split into 3 or more (such as "CDE").)
------------------------------------------------------------------------
Dan Ramaley Dial Center 118, Drake
University
Network Programmer/Analyst 2407 Carpenter Ave
+1 515 271- 4540 Des Moines IA 50311 USA
_______________________________________________
Cialug mailing list
Cialug at cialug.org
http://cialug.org/mailman/listinfo/cialug
More information about the Cialug
mailing list