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 = [EMAIL PROTECTED]; foreach (@ARGV) { if (/^-/) { if ($_ eq '-f') { $list = [EMAIL PROTECTED]; } elsif ($_ eq '-f2') { $list = [EMAIL PROTECTED]; } else { $list = ''; } } else { push @$list, $_ if $list; } } my $pattern = join ' ', @pattern; my $replacement = join ' ', @replacement; @ARGV = @files; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>