perl regexp question

I

IcyMint

Hi, I'm trying to change the contents of a file base on the substitute
list from the other file. I'd copied my progress over here, albeit with
some changes. The content file format is: #name group OR #name \n+
group
The script is working fine, but I want it to compare if the group name
is correct. If it is incorrect, change it, and count how many times it
had been changed. What I'm trying to do is something like:
while ($content =~ /\n\#(\S+)\b\s+(\S+)/g) {
my($name,$group) = ($1,$2);
unless ($group eq $data{$name}) {
$2 = $data{$name};
$count{$name}++;
}
}
It's not a valid code, it gave me errors, but i'm just including it so
that maybe you'll understand what I'm trying to achieve. Can you help
me with this? Thank you!


open(FILE,"content.txt") or die "Cannot open file content.txt~ $!\n";
open(DATA,"data.txt") or die "Cannot open file data.txt~ $!\n";

undef $/;
my $content = <FILE>;
my $data = <DATA>;

while ($data =~ /\nsubstitute\s+(\S+)\s+(\S+)/g) {
$data{$1} = $2;
}

foreach my $key (keys %data) {
$content =~ s/\n\#$key\b\s*\S+/\n\#$key $data{$key}/g;
$content =~ s/\n\#$key\b\s*\n\+\s+\S+/\n\#$key\n\+\s$data{$key}/g;
}


The content.txt(modified version) file looks something like this:

#benjamin TB3
#desmond TG2
#terrence TE1
#abigail_lim_suet_ching
+ KR8
 
T

Tad McClellan

IcyMint said:
Hi, I'm trying to change the contents of a file base on the substitute
list from the other file. I'd copied my progress over here, albeit with
some changes. The content file format is: #name group OR #name \n+
group
The script is working fine, but I want it to compare if the group name
is correct. If it is incorrect, change it, and count how many times it
had been changed. What I'm trying to do is something like:
while ($content =~ /\n\#(\S+)\b\s+(\S+)/g) {
my($name,$group) = ($1,$2);
unless ($group eq $data{$name}) {
$2 = $data{$name};
$count{$name}++;
}
}
It's not a valid code, it gave me errors, but i'm just including it so
that maybe you'll understand what I'm trying to achieve.


It is "too late" to easily substitute with your while loop above.

Replace the whole loop with (untested):

$content =~ s{\n\#(\S+)\b\s+(\S+)}
{
my($name,$group) = ($1,$2);
if ( $group eq $data{$name}) {
$group; # replace it with itself
}
else {
$count{$name}++;
$data{$name}; # replace it from the hash
}
}ge;
 
T

Tad McClellan

Tad McClellan said:
Replace the whole loop with (untested):

$content =~ s{\n\#(\S+)\b\s+(\S+)}


Oops. Better capture the "other stuff" so it can be put back in:

$content =~ s{(\n#(\S+)\b\s+)(\S+)}
^ ^
{
my($name,$group) = ($1,$2);


my($name,$group) = ($2,$3);

if ( $group eq $data{$name}) {
$group; # replace it with itself


"$1$group"; # replace it with itself

}
else {
$count{$name}++;
$data{$name}; # replace it from the hash


"$1$data{$name}"; # replace it from the hash
 
J

John W. Krahn

Jim said:
You are ignoring the substitution on the first line of the file, so you
need a blank line at the beginning of the file. Better is to use the \G
metasymbol and m modifier. Even simpler and better is to read the file
one line at a time and extract the substitution strings from a single
line one-at-a-time:

my %data;
while (<DATA>) {
if( m{ \A \s* substitute \s+ (\S+) \s+ (\S+) }x ) {
$data{$1} = $2;
}
}

Or just assign the data directly to the hash:

my %data = $data =~ /^substitute\s+(\S+)\s+(\S+)/mg;




John
 
I

IcyMint

Hi Jim, thanks for pointing out all my mistakes and for all the great
advices on correcting them! I didn't notice my careless mistakes on the
substitution thing. You'd saved me from a lot of troubles later! I'll
need to work more on my PERL, it's still in an infancy stage...

John, thanks for the tips, it's nice to know there are other ways of
doing the same thing.

Tad, the example you'd given me is what I'm looking for. After a little
tweak, it works! Thanks a lot!

Thanks again!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top