On Oct 25, 12:01 pm, [EMAIL PROTECTED] (Minky Arora) wrote: > I need to make multiple substitutions in a file.There could be a > situation where there are more than one substitution on a single > line.I want to count the total # of substituitions.I need to use > subroutines:
Says who? Is this a homework assignment? If so why aren't you asking your teacher for help? > I have 3 questions: > > 1) Do I have to make 3 separate Regex. It depends on what you are substituting and replacing. If you're searching for three different things and replacing them all with the same thing, then no. Just use the | alternation regexp character. > How can it be done in a single line? Why do you want it to be? > 2)How to use Subroutines with this? If I just want the Sub to > substitute the value and return the line? The same way you use subroutines in any other way. What is your question here? Do you not know how to write a subroutine? Do you not know how to call a subroutine? Have you read `perldoc perlsub` yet? > 3)Is it a good practice to open the file using Sub? If so, how to do that? This question is nonsensical. Try again. > !/usr/bin/perl -w You forgot the # in front of the shebang. This code does not compile. > use strict; > my $line; > my ($count,$count1,$count3,$count2); > > open FILE, "data.txt" or die"cnt open $!"; > foreach $LINE(<FILE>) { Don't use foreach() to process a file line-by-line. Use while(). > if ($var =~ s/cellomics/array/g) { > $count1++; > } This counts the number of lines in which this substitution was made. It does not count the number of substitutions that were made. That is, if this line contained two instances of cellomics that were both replaced with array, $count1 would only go up by one, not by two. If that's what you want, fine. If not, change to: $count1 += ($var =~ s/cellomics/array/g); > if ($var=~ s/perkin almer/janus/g) { > $count2++ > } > if ($var =~ s/dell/laptop/g) { > $count3++ > }} Same comments here. Paul Lalli -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/