help regex and substitutions

Discussion in 'Perl Misc' started by john swilting, Aug 31, 2007.

  1. I recopied the chaphitre 5
    #!/usr/bin/perl -w

    use strict;
    use diagnostics;

    my @conf = ('XferMethod',
    'rsync',
    'XferLogLevel',
    '1',
    'RSyncShare',
    '___1___',##they is these 2 pattern
    'ClientNameAlias',
    '___2___');##they is pattern


    print @conf,"\n";
    my @substitution = @conf;
    my $motif =qr/___[1-9][0-9]?___/is;
    my @regexes = ();
    my @motif = ();
    foreach $motif ( @motif ) {
    push @regexes, qr/$motif/;
    }

    foreach my $elem ( @substitution ) {
    foreach my $re ( @regexes ) {##the loop however turns well#scalar(@regexes
    if ( $elem =~ /$re/) {##one do not arrive to this line
    print "$elem egal $re\nentrer la valeur\n";
    chop ($elem =<STDIN>);
    print "nouvelle valeur de",$elem,"\n";
    }
    }
    }
     
    john swilting, Aug 31, 2007
    #1
    1. Advertising

  2. Jim Gibson wrote:

    > In article <46d83c60$0$27378$>, john swilting
    > <> wrote:
    >
    >> I recopied the chaphitre 5

    >
    > In what book?
    >
    >> #!/usr/bin/perl -w

    >
    > use warnings; # preferred these days
    >
    >>
    >> use strict;
    >> use diagnostics;
    >>
    >> my @conf = ('XferMethod',
    >> 'rsync',
    >> 'XferLogLevel',
    >> '1',
    >> 'RSyncShare',
    >> '___1___',##they is these 2 pattern
    >> 'ClientNameAlias',
    >> '___2___');##they is pattern
    >>
    >>
    >> print @conf,"\n";
    >> my @substitution = @conf;
    >> my $motif =qr/___[1-9][0-9]?___/is;
    >> my @regexes = ();
    >> my @motif = ();

    >
    > The array @motif is empty.
    >
    >> foreach $motif ( @motif ) {
    >> push @regexes, qr/$motif/;
    >> }

    >
    > You are iterating over the empty array @motif, assigning each value
    > therein to the variable $motif. At the end of this loop, since @motif
    > has no values, the array @regexes will also be empty.
    >
    > Perhaps you want, instead of the loop, the following:
    >
    > push( @regexes, $motif );
    >
    >>
    >> foreach my $elem ( @substitution ) {
    >> foreach my $re ( @regexes ) {##the loop however turns
    >> well#scalar(@regexes
    >> if ( $elem =~ /$re/) {##one do not arrive to this line
    >> print "$elem egal $re\nentrer la valeur\n";
    >> chop ($elem =<STDIN>);
    >> print "nouvelle valeur de",$elem,"\n";
    >> }
    >> }
    >> }
    >>

    >
    > The inner statements of the loop on @regexes will never be executed,
    > because @regexes has no values.
    >

    the book is programmation en perl 3eme edition
    with the push (@regexes,$motif)
    the substitution is well

    how to make to improve my code... I look at closely the precis examples
    handbook O reilly
     
    john swilting, Aug 31, 2007
    #2
    1. Advertising

  3. john swilting wrote:

    > Jim Gibson wrote:
    >
    >> In article <46d83c60$0$27378$>, john swilting
    >> <> wrote:
    >>
    >>> I recopied the chaphitre 5

    >>
    >> In what book?


    > the book is programmation en perl 3eme edition
    > with the push (@regexes,$motif)
    > the substitution is well
    >
    > how to make to improve my code... I look at closely the precis examples
    > handbook O reilly

    #!/usr/bin/perl -w

    use strict;
    use diagnostics;
    use warnings;

    my @conf = ('XferMethod',
    'rsync',
    'XferLogLevel',
    '1',
    'RSyncShare',
    '___1___',
    'ClientNameAlias',
    '___2___');


    print @conf,"\n";
    my @substitution = @conf;
    my $motif =qr/(___[1-9][0-9]?___)/is;
    my @regexes = ();
    ##my @motif = ();
    ##foreach $motif ( @motif ) {
    ## push (@regexes,qr/$motif/);
    ## }
    push (@regexes,$motif);
    foreach my $elem ( @substitution ) {
    foreach my $re ( @regexes ) {
    if ( $elem =~ /$re/) {
    print "$elem egal $re\nentrer la valeur\n";
    chop ($elem =<STDIN>);
    print "nouvelle valeur de",$elem,"\n";
    }
    }
    }


    how to do improves my code
     
    john swilting, Aug 31, 2007
    #3
  4. john swilting

    Klaus Guest

    On Aug 31, 7:15 pm, john swilting <> wrote:
    > how to make to improve my code...
    >
    > #!/usr/bin/perl -w
    >
    > use strict;
    > use diagnostics;


    If you can afford, get rid of the "use diagnostics" to improve
    execution speed.

    > use warnings;
    >
    > my @conf = ('XferMethod',
    > 'rsync',
    > 'XferLogLevel',
    > '1',
    > 'RSyncShare',
    > '___1___',
    > 'ClientNameAlias',
    > '___2___');
    >
    > print @conf,"\n";
    > my @substitution = @conf;
    > my $motif =qr/(___[1-9][0-9]?___)/is;
    > my @regexes = ();


    > ##my @motif = ();
    > ##foreach $motif ( @motif ) {
    > ## push (@regexes,qr/$motif/);
    > ## }


    You could remove the commented out code altogether from your program,
    it is confusing.

    > push (@regexes,$motif);
    > foreach my $elem ( @substitution ) {
    > foreach my $re ( @regexes ) {
    > if ( $elem =~ /$re/) {
    > print "$elem egal $re\nentrer la valeur\n";
    > chop ($elem =<STDIN>);


    You could use chomp instead of chomp.
    perldoc -f chomp
    This safer version of chop removes any trailing string that
    corresponds to the current value of $/

    > print "nouvelle valeur de",$elem,"\n";
    > }
    > }
    > }
     
    Klaus, Aug 31, 2007
    #4
  5. john swilting <> wrote:


    > my $motif =qr/___[1-9][0-9]?___/is;



    That is equivalent to:

    my $motif =qr/___[1-9][0-9]?___/;

    Both of the modifiers are no-ops. They do nothing.

    m//i ignores case. Where can your pattern match letters so that
    their case can be ignored? Nowhere.

    m//s makes dot in your pattern match a newline. Where is the dot in
    your pattern that is being modified?

    You should not use a modifier unless you know what that modifier does,
    and why it is needed in your regex.


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad McClellan, Aug 31, 2007
    #5
    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. RJGraham
    Replies:
    6
    Views:
    511
    RJGraham
    Jun 28, 2004
  2. Tony Eva
    Replies:
    1
    Views:
    499
    Jeff Epler
    Nov 9, 2003
  3. Replies:
    3
    Views:
    835
    Reedick, Andrew
    Jul 1, 2008
  4. Ivan Shmakov

    "walk over," and XPath-based substitutions?

    Ivan Shmakov, Apr 6, 2013, in forum: Perl Misc
    Replies:
    3
    Views:
    253
    Ivan Shmakov
    Apr 7, 2013
  5. Ivan Shmakov
    Replies:
    2
    Views:
    537
    Joe Kesselman
    Apr 7, 2013
Loading...

Share This Page