Moon, John wrote: > > perl -e '@a=("frc.apmt","frc_ff.apmt");print join(q{,}, map(&subt($_), > @a)), "\n"; > sub subt {my ($a) = @_; $a=~s/^(.*)\..*/$1/; print "a=$a\n"; > return $a;}' > perl -e '@a=("frc.apmt","frc_ff.apmt");print join(q{,}, > map(s/^(.*)\..*/$1/, @a)), "\n"; ' > perl -e '@a=("frc.apmt","frc_ff.apmt");print join(q{,}, > map(s/^(.*)\..*/\1/, @a)), "\n"; ' > > Can someone explain why the last two examples don't product the same > output as the first?
Hi John Here's your code again, laid out a little more visibly: use strict; use warnings; my @a; @a = qw/frc.apmt frc_ff.apmt/; print join(q{,}, map(&subt($_), @a)), "\n\n"; @a = qw/frc.apmt frc_ff.apmt/; print join(q{,}, map(s/^(.*)\..*/$1/, @a)), "\n\n"; @a = qw/frc.apmt frc_ff.apmt/; print join(q{,}, map(s/^(.*)\..*/\1/, @a)), "\n\n"; sub subt { my ($a) = @_; $a =~ s/^(.*)\..*/$1/; print "a=$a\n"; return $a; } and the output is: \1 better written as $1 at E:\Perl\source\x.pl line 13. a=frc a=frc_ff frc,frc_ff 1,1 1,1 In the latter two cases your call to map generates a list consisting of the return values of the subtitution operator acting on each list element. Those values are the number of substitutions made on the string, so all the values in your array are altered in place and your result is a list of 1s as one substitution was done on each element. (You will see that adding 'use warnings' caused Perl to chastise you for using \1 in the last case instead of the correct $1.) To write this correctly, use something like @a = qw/frc.apmt frc_ff.apmt/; print join(q{,}, map /([^.]*)/, @a), "\n\n"; will do the trick. The return value of the match operator in list context is the value of the captured subexpressions, which in this case is all the non-dot characters from the beginning of the string. Note that this alternative doesn't modify the original array at all. HTH, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>