Just a basic wildcard

Discussion in 'Perl Misc' started by guilhy.nogueira@gmail.com, Jan 31, 2006.

  1. Guest

    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.
     
    , Jan 31, 2006
    #1
    1. Advertising

  2. Matt Garrish Guest

    <> wrote in message
    news:...
    > 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:
    >


    <snip code not written by OP>

    >
    > 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
     
    Matt Garrish, Jan 31, 2006
    #2
    1. Advertising

  3. Xicheng Guest

    wrote:
    > 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.
     
    Xicheng, Jan 31, 2006
    #3
  4. "" <> wrote in
    news::

    > 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"/>

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Feb 1, 2006
    #4
  5. Dr.Ruud Guest

    Xicheng schreef:

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


    Alternative:

    s~(<owner\s+name=").*?("/>)~$1$2~msg

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Feb 1, 2006
    #5
  6. Uri Guttman Guest

    >>>>> "gnc" == guilhy nogueira@gmail com <> writes:

    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

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Feb 1, 2006
    #6
  7. Ian Wilson Guest

    wrote:
    > 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.


    <snip script>

    >
    > 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
     
    Ian Wilson, Feb 1, 2006
    #7
  8. Guest

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

    Again... THANK YOU!






    Xicheng wrote:
    > wrote:
    > > 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.
     
    , Feb 1, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Naveen M

    using FTP Wildcard in asp.net code

    Naveen M, Jul 21, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    442
    Christopher Frazier
    Jul 23, 2003
  2. walterbyrd
    Replies:
    1
    Views:
    381
    Damjan
    Apr 10, 2006
  3. Ark Khasin

    Is it just me or just Microsoft?

    Ark Khasin, Jul 15, 2007, in forum: C++
    Replies:
    24
    Views:
    772
    Lionel B
    Jul 16, 2007
  4. Ark Khasin

    Is it just me or just Microsoft?

    Ark Khasin, Jul 15, 2007, in forum: C Programming
    Replies:
    42
    Views:
    1,148
    user923005
    Jul 17, 2007
  5. Replies:
    7
    Views:
    877
Loading...

Share This Page