Re: multi-word substitution

From: Rob Dixon (rob_at_dixon.port995.com)
Date: 02/24/04

  • Next message: Rob Dixon: "Re: scanning module"
    To: beginners@perl.org
    Date: Tue, 24 Feb 2004 13:06:03 -0000
    
    

    Adriano Allora wrote:
    >
    > I want to create a script which can substitute a more-than-one-word
    > substitution with more than other one from command line in a large
    > amount of files.
    >
    > My idea:
    > $ perl command words to be substitute -f words with which substitute
    > -f2 file file file...
    >
    > (-f marks the end (the italian word for end is "fine") for the first
    > group of words and -f2 the end of second group).
    >
    > The script can see and print al my variables (in particular $pattern
    > and $replacement) but doesn't use them, and I'm sure it's something of
    > simple but (for my eyes) invisible.
    >
    > Thanks a lot,
    >
    > alladr
    >
    >
    >
    > ===========================THE SCRIPT========================
    >
    > #!/usr/bin/perl -w
    >
    > use strict;
    > my ($var, $mark1, $mark2, $pattern, $replacement);
    > foreach $ARGV (@ARGV)
    > {
    > $mark1 = $var if ($ARGV eq "-f");
    > $mark2 = $var if ($ARGV eq "-f2");
    > $var++;
    > }
    > $var = 0;
    > for($var = 0; $var < $mark1; $var++)
    > {
    > $pattern .= "$ARGV[$var] ";
    > }
    > $mark1++;
    > for($var = $mark1; $var < $mark2; $var++)
    > {
    > $replacement .= "$ARGV[$var] ";
    > }
    > $var -= $mark2;
    > $mark2++;
    > chomp $pattern;
    > chomp $replacement;
    >
    > ################
    > # here my problem: the script see my $pattern and my $replacement but
    > doesn't use them
    > #
    > # someone can help me?
    > ################
    >
    > $^I = '';
    > splice @ARGV, 0, $mark2;
    >
    > while(<>){
    > tr/\015\012/\n/s;
    > s/$pattern/$replacement/g;
    > print;
    > print STDOUT ++$var, " - I cleaned $ARGV\n" if (eof);
    > }

    Hi.

    I'm not very happy with your command structure: command-line switches are
    usually used to introduce parameter values rather than to terminate them.

    However, your code will substitute the string 'words to be substitute'
    with 'words with which substitute' and print the changed file to STDOUT,
    unless there are any regex special characters in either string. Is that
    what you intended? I got the impression at first that you wanted to
    change any one of a list of words. Perhaps your problem is that the
    pattern string will always have a trailing space added before the search
    is performed? You may like to try the code beloe to build your parameter
    data instead.

    HTH,

    Rob

    my @pattern;
    my @replacement;
    my @files;
    my $list = \@pattern;

    foreach (@ARGV) {
      if (/^-/) {
        if ($_ eq '-f') {
          $list = \@replacement;
        }
        elsif ($_ eq '-f2') {
          $list = \@files;
        }
        else {
          $list = '';
        }
      }
      else {
        push @$list, $_ if $list;
      }
    }

    my $pattern = join ' ', @pattern;
    my $replacement = join ' ', @replacement;
    @ARGV = @files;


  • Next message: Rob Dixon: "Re: scanning module"

    Relevant Pages

    • Re: Using foreach loop to create radiobutton menu
      ... Your foreach loop is not the problem, the problem is how you define the -command. ... The only possible thing the interpreter can do is substitute the current value of range, which is likely the last value once your loop exited. ... One choice is double quotes. ... The double quotes means that $range gets expanded while in the loop, long before the puts command actually runs. ...
      (comp.lang.tcl)
    • Re: substitute a specific string
      ... On 2004-06-18, Andrea Arquint wrote: ... > I would like to substitute a specific string. ... I would like to use a command which does search the whole file ...
      (comp.unix.shell)
    • do loop with msg box
      ... Looking for a "neater coding" method using a Do Loop ... to substitute and clean up code that already works. ... The user clicks a command button to hide the subform. ...
      (microsoft.public.access.formscoding)
    • Re: Solving non linear equations
      ... I tried to solve these equations using 'fsolve' command, ... First substitute ... Then substitute the left hand expression in this equation ... It is easy to see that there are three triplet solutions ...
      (comp.soft-sys.matlab)
    • Re: copy as root
      ... what the command is to substitute,,,:) ... Better to use gksudo to launch graphical apps with administrative ... the command below substituting your username for user.' ...
      (Ubuntu)

    Loading