在 Perl 中返回数组

发布于 2024-12-17 22:50:37 字数 1724 浏览 4 评论 0原文

我正在 Perl 中开发一个递归文件查找函数,该函数应该返回一个文件名数组。然而,当我尝试打印它们时,我只得到 0。我做错了什么?

use strict;
use File::Basename;
use constant debug => 0;

sub isdir {
    return (-d $_[0]);
}

sub isfile {
    return (-f $_[0]);
}

my $level = 0;

#my @fns = ();

sub getfn {
    my @fns = ();
    my($file, $path) = @_;
    my (undef, undef, $ext) = fileparse($file, qr"\.[^.]+$");
    $level++;
    print "-->>getfn($level): $file : $path\n" if debug;
    print "arg:\t$file\t$path ($ext)\n" if debug;
    if ($ext eq ".bragi") {
        open my $FILE, "<", "$path/$file" or die "Failed to open $path/$file: $!";
        my @lines = <$FILE>;
        close $FILE;
        foreach my $line (@lines) {
            chomp($line);
            my $fullpath = "$path/$line";
            print "---- $fullpath\n" if debug;
            if (isfile($fullpath)) {
                #print "file:\t$fullpath\n";
                push(@fns, $fullpath);
                getfn($line, $path);
            }
            elsif (isdir($fullpath)) {
                #print "DIR:\t$fullpath\n";
                opendir my ($dh), $fullpath or
                    die "$fullpath does not exist or is not a directory: $!";
                my @files = readdir $dh;
                closedir $dh;
                foreach my $f (@files) {
                    getfn($f, "$fullpath");
                }
            }
        }
    }
    print "<<--getfn($level)\n" if debug;
    $level--;
    #print @fns;
    return @fns;
}


foreach my $f (<*>) {
    #print "fn: ".$f."\n";
    my (undef, undef, $ext) = fileparse($f, qr"\.[^.]+$");
    if ($ext eq ".bragi") {
    print &getfn($f, $ENV{PWD})."\n";
    }
}

I'm working on a recursive file-finding function in Perl that's supposed to return an array of filenames. What happens, though, is when I try to print them, I just get 0. What am I doing wrong?

use strict;
use File::Basename;
use constant debug => 0;

sub isdir {
    return (-d $_[0]);
}

sub isfile {
    return (-f $_[0]);
}

my $level = 0;

#my @fns = ();

sub getfn {
    my @fns = ();
    my($file, $path) = @_;
    my (undef, undef, $ext) = fileparse($file, qr"\.[^.]+$");
    $level++;
    print "-->>getfn($level): $file : $path\n" if debug;
    print "arg:\t$file\t$path ($ext)\n" if debug;
    if ($ext eq ".bragi") {
        open my $FILE, "<", "$path/$file" or die "Failed to open $path/$file: $!";
        my @lines = <$FILE>;
        close $FILE;
        foreach my $line (@lines) {
            chomp($line);
            my $fullpath = "$path/$line";
            print "---- $fullpath\n" if debug;
            if (isfile($fullpath)) {
                #print "file:\t$fullpath\n";
                push(@fns, $fullpath);
                getfn($line, $path);
            }
            elsif (isdir($fullpath)) {
                #print "DIR:\t$fullpath\n";
                opendir my ($dh), $fullpath or
                    die "$fullpath does not exist or is not a directory: $!";
                my @files = readdir $dh;
                closedir $dh;
                foreach my $f (@files) {
                    getfn($f, "$fullpath");
                }
            }
        }
    }
    print "<<--getfn($level)\n" if debug;
    $level--;
    #print @fns;
    return @fns;
}


foreach my $f (<*>) {
    #print "fn: ".$f."\n";
    my (undef, undef, $ext) = fileparse($f, qr"\.[^.]+$");
    if ($ext eq ".bragi") {
    print &getfn($f, $ENV{PWD})."\n";
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相思故 2024-12-24 22:50:37

这里的主要问题是这样的一行:

getfn($line, $path);

并没有真正做任何事情。它找到子目录中的所有文件,但随后完全丢弃它们。您需要将其返回值合并到外部调用的 @fns 中。

第二个问题是:

print &getfn($f, $ENV{PWD})."\n";

强制返回的数组被视为标量,因此它打印数组元素的数量,而不是数组元素的内容。你可能想要这样的东西:

print "$_\n" foreach getfn($f, $ENV{PWD});

The main problem here is that a line like this:

getfn($line, $path);

doesn't really do anything. It finds all the files in the subdirectory, but then it completely discards them. You need to incorporate its return value into your outer call's @fns.

A second problem is that this:

print &getfn($f, $ENV{PWD})."\n";

forces the returned array to be treated as a scalar, so it prints the number of array elements rather than the contents of the array elements. You probably want something like this:

print "$_\n" foreach getfn($f, $ENV{PWD});
顾北清歌寒 2024-12-24 22:50:37

当您递归调用 getfn() 时,您永远不会将返回的数组分配给任何内容。您唯一的任务是:

my @fns = ();

在函数的顶部,这就是返回的内容。

You never assign the returned array to anything when you call getfn() recursively. Your only assignment is:

my @fns = ();

at the top of the function, and that's what is getting returned.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文