Ron Smith wrote:
<snip>
#!/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";
That line assigns to @basenames the extracted basename from the last path only, i.e. the basenames extracted during previous iterations are not kept. You probably mean:
push @basenames, $1;
} }
Here the loop is finished, but nothing is returned from the subroutine.
return @basenames;
}
Everything looks OK when I do the test prints. But, I'm not getting output from the subroutine. What's the correct way to get this return?
My mods above would work, but it's a little odd to assign to a file scoped variable within a function, have the function return it and reassign to it the list returned from the function... You should declare a separate variable within the function instead:
sub basenames { my @names; foreach (@_) { if ( /(\w+)\.\d+\.\w+$/ ) { push @names, $1; } } @names; }
Also, there are of course other ways to solve the problem. The whole program could for instance be replaced with:
#!/usr/bin/perl use strict; use warnings;
my @basenames = map { /(\w+)\.\d+\.\w+$/; $1 or () } `dir /b/s`;
The OS is Window$.
That's noted. For a more portable solution, you may want to explore modules such as File::Find or File::Finder.
-- Gunnar Hjalmarsson Email: http://www.gunnar.cc/cgi-bin/contact.pl
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>