Just a basic wildcard

G

guilhy.nogueira

Hello!

I'm new to Perl programming, and i want a wildcard to change the
following line:

<owner name="Hopugop"/>

to

<owner name=""/>

in many differents files.

i have a script here:

----------------------


for $file (glob "../data/houses/*")
{
next unless $file =~ /\.xml$/;

open(FILE, $file);
@content = <FILE>;
$_ = "@content";
close(FILE);

s/<owner name="Hopugop"\/>/<owner name=""\/>/;

open(FILE, ">$file");
print FILE;
close(FILE);
}



----------------------

But, with this script, i can only change "Hopugop" to "", not like
another names...

if you could help me... =D

PS.: This is an Open Tibia Server (OTServer) script... If you have
another ones... please send it to me.
 
M

Matt Garrish

Hello!

I'm new to Perl programming, and i want a wildcard to change the
following line:

<owner name="Hopugop"/>

to

<owner name=""/>

in many differents files.

i have a script here:

PS.: This is an Open Tibia Server (OTServer) script... If you have
another ones... please send it to me.

This is not a forum for free handouts. I'd give you the answer, but I'm not
feeling very generous (mostly because you're using regexes for xml).
Instead, I'll recommend you take a look at perlre and discover how a negated
character class can do what you're after. I just hope you're certain there
aren't any owner elements you don't want to do this to, because regexes are
not discriminating.

Matt
 
X

Xicheng

Hello!
I'm new to Perl programming, and i want a wildcard to change the
following line:
<owner name="Hopugop"/>
to
<owner name=""/>

in many differents files.

i have a script here:

----------------------


for $file (glob "../data/houses/*")
{http://groups.google.com/group/comp...ad/thread/574cb10a1fc07499/fa9af856cb84077d?&
next unless $file =~ /\.xml$/;

open(FILE, $file);
@content = <FILE>;
$_ = "@content";
close(FILE);

s/<owner name="Hopugop"\/>/<owner name=""\/>/;
change this to:

s{<owner name="[^"]*"/>}{<owner name=""/>}sg

you may also need to add 's' modifier as you slurp in the whole file..

Xicheng
 
A

A. Sinan Unur

I'm new to Perl programming, and i want a wildcard to change the
following line:

<owner name="Hopugop"/>

to

<owner name=""/>

Except for very specific cases, regular expressions are not the right
tool for the job of parsing XML files.


use strict;
use warnings;
for $file (glob "../data/houses/*")
{
next unless $file =~ /\.xml$/;

Ahem ... since you want to use glob, why not

while (defined( my $xml_file = glob '*.xml') ) {
open(FILE, $file);

Always, yes, always check if open succeeded:

open my $xml, '<', $xml_file
or die "Cannot open '$xml_file': $!";
@content = <FILE>;
$_ = "@content";

*Sigh*. You are mucking about with $_ without localising it. Not a good
habit. But, if you just want to slurp the whole while, consider using
File::Slurp.

use File::Slurp;

....

my $contents = read_file( $xml_file);
close(FILE);

s/<owner name="Hopugop"\/>/<owner name=""\/>/;

How many times can this occur?
open(FILE, ">$file");

open my $out, '>', $xml_file
or die "Cannot open '$xml_file' for writing: $!";

or, just use write_file from File::Slurp


So, the script can be reduced to:

#!/usr/bin/perl

use strict;
use warnings;

use File::Slurp;

my ($find, $replace) = @ARGV;

XML_FILE: while (defined( my $xml_file = glob '*.xml') ) {
my $contents = read_file( $xml_file );
$contents =~ s{<owner name="$find"/>}{<owner name="$replace"/>};
unless (rename $xml_file => "$xml_file.bak") {
warn "Cannot backup '$xml_file' ... skipping\n";
next XML_FILE;
}
write_file($xml_file => $contents)
or warn "Could not write '$xml_file': $!";
}

__END__

D:\Home\asu1\UseNet\clpmisc\x> cat test.xml
<owner name="Nanis"/>

D:\Home\asu1\UseNet\clpmisc\x> x Nanis Sinan

D:\Home\asu1\UseNet\clpmisc\x> cat test.xml
<owner name="Sinan"/>

D:\Home\asu1\UseNet\clpmisc\x> dir
Volume in drive D is DATA
Volume Serial Number is ACC0-AC7B

Directory of D:\Home\asu1\UseNet\clpmisc\x

01/31/2006 06:41 PM <DIR> .
01/31/2006 06:41 PM <DIR> ..
01/31/2006 07:11 PM 25 test.xml
01/31/2006 07:10 PM 25 test.xml.bak
01/31/2006 07:08 PM 514 x.pl
3 File(s) 564 bytes
2 Dir(s) 4,516,642,816 bytes free

D:\Home\asu1\UseNet\clpmisc\x> cat test.xml.bak
<owner name="Nanis"/>
 
U

Uri Guttman

gnc> open(FILE, $file);
gnc> @content = <FILE>;
gnc> $_ = "@content";

all of the other major flaws in your code have been covered by
others. but this one still remains. did you look at the output of your
program carefully? did you notice any extra spaces that weren't there
before? the above line put them in since arrays interpolate into strings
with a blank character as the separator by default.

uri
 
I

Ian Wilson

Hello!

I'm new to Perl programming, and i want a wildcard to change the
following line:

<owner name="Hopugop"/>
to
<owner name=""/>
in many differents files.

But, with this script, i can only change "Hopugop" to "", not like
another names...

Isn't this equivalent to

perl -p -i -e 's/<owner name="[^"]*">/<owner name="">/g' *.xml
 
G

guilhy.nogueira

Thanks a lot! It worked! I've tried many others... but his one is just
perfect...

Again... THANK YOU!





Hello!
I'm new to Perl programming, and i want a wildcard to change the
following line:
<owner name="Hopugop"/>
to
<owner name=""/>

in many differents files.

i have a script here:

----------------------


for $file (glob "../data/houses/*")
{http://groups.google.com/group/comp...ad/thread/574cb10a1fc07499/fa9af856cb84077d?&
next unless $file =~ /\.xml$/;

open(FILE, $file);
@content = <FILE>;
$_ = "@content";
close(FILE);

s/<owner name="Hopugop"\/>/<owner name=""\/>/;
change this to:

s{<owner name="[^"]*"/>}{<owner name=""/>}sg

you may also need to add 's' modifier as you slurp in the whole file..

Xicheng
open(FILE, ">$file");
print FILE;
close(FILE);
}



----------------------

But, with this script, i can only change "Hopugop" to "", not like
another names...

if you could help me... =D

PS.: This is an Open Tibia Server (OTServer) script... If you have
another ones... please send it to me.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top