On Wed, 2004-10-20 at 23:19, Ron Smith wrote: > The following is the code:
> #!/usr/bin/perl -w > use strict; > my @paths = `dir /b/s`; # print > @paths; > my @basenames = &basenames(@paths); > sub basenames { > foreach (@_) { > if ($_ =~ /(\w+)\.\d+\.\w+$/) { > @basenames = $1; # print > "@basenames\n"; > } > } > } First, while it's allowable, it seems to me that you're asking for trouble by using the same name multiple times. Perl may not have difficulty keeping sub var, $var, @var and @var declared again at a different scope separated, but programmers sure do. For instance, do you mean for the array @basenames inside the subroutine to be the same as the array @basenames that you declared outside the subroutine? If so, why are you trying to assign a value to it when it's already (theoretically) being populated inside the subroutine? I say theoretically because when you assign a scalar to an array via '=', you're essentially creating a new array with one element. Any values that were in the array are lost. If you're trying to add an additional element to the array, you need to use push (to add to the end of the array) or unshift (to add to the beginning of the array.) If you intended to use the same variable inside and outside the subroutine (just one verion of @basenames), then don't bother assigning to the variable. Just call the subroutine. I don't recommend it, but it will work. On the other hand, if you intended to have two different variables, change the name of one of them (and you'll need to declare it with "my" inside the subroutine.). Then explicitly return the array from the subroutine. Something like this (untested code!): bad way #!/usr/bin/perl -w use strict; my @paths = `dir /b/s`; my @basenames; procbasenames(@paths); sub procbasenames { foreach (@_) { if ($_ =~ /(\w+)\.\d+\.\w+$/) { push @basenames, $1; } } } better way #!/usr/bin/perl -w use strict; my @paths = `dir /b/s`; my @basenames = procbasenames(@paths); sub procbasenames { my @basenamematches; foreach (@_) { if ($_ =~ /(\w+)\.\d+\.\w+$/) { push @basenamematches, $1; } } return @basenamematches; } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>