Perl 从 sub 返回 multiData

发布于 2024-12-15 13:55:57 字数 2404 浏览 0 评论 0原文

我是 Perl 新手,在找出从子程序返回多个数组的最佳方法时遇到一些问题。这是我的初学者代码。

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use List::Util;
use Fcntl 'O_RDONLY';
use Tie::File;
use YAML qw();

my $digitData   = 'digitData.txt';
my $alphaData = 'alphaData.txt';

my (@dataA, @dataN) = dataMod();
print Dumper(@dataA);
print Dumper(@dataN);

sub dataMod {
    my (@alphaData, @numData);
    my $fileCount = `wc -l < $alphaData`;;
    chomp $fileCount;
    my $history   = eval {YAML::LoadFile('history.yaml')} || {};

    $history->{$_} && --$history->{$_} for keys %$history;

    tie my @alphas, 'Tie::File', $alphaData, mode => O_RDONLY;
    tie my @nums, 'Tie::File', $digitData, mode => O_RDONLY;

    LINES: for (1 .. $fileCount) {
            my @alphaPool = @alphas;
            my $pair;

            while (@alphaPool) {
                    my @numberPool = @nums;
                    my $tryAlpha = splice @alphaPool, rand(@alphaPool), 1;

                    while (@numberPool) {
                            my $tryNum = splice @numberPool, rand(@numberPool), 1;

                            next if $history->{"$tryAlpha|$tryNum"};

                            @alphas = grep {$_ ne $tryAlpha} @alphas;
                            @numberPool = grep {$_ != $tryNum} @numberPool;
                            #print "$tryAlpha $tryNum\n";
                            push @alphaData, $tryAlpha;
                            push @numData, $tryNum;
                            $history->{"$tryAlpha|$tryNum"} = 5;
                            next LINES;
                    }

                    @alphas = grep {$_ ne $tryAlpha} @alphas;
            }
    }
    YAML::DumpFile('history.yaml', $history);
    return(@alphaData, @numData);
}

我无法找出从子例程返回数据的最佳方法。我需要保存或能够打印这两个变量的数据: $tryAlpha $tryNum 返回后一起打印。

目前它返回每个不相连的值。看起来只有一个数组有数据?

当前输出采用以下格式:

$VAR1 = cellCpe2
$VAR2 = stemClearSte
$VAR3 = OctuStemPr2
$VAR4 = 10
$VAR5 = 30
$VAR6 = 20

问题是,我想以在子例程内执行以下打印语句时返回的格式使用它: 打印“$tryAlpha $tryNum\n”;

对于此打印语句的结果,我需要能够对打印语句中的数据使用相同的逻辑: 即: $varForAlphaData $varForNumData

cellCpe2 10
stemClearSte 30
OctuStemPr2 20

出于测试目的,我使用两个文件 *digitData.txt:包含三个单词cellCpe2、stemClearSte、OctuStemPr2。每行一个

*alphaData.txt:包含 10、20、30、40、50、60。每行一个

不确定此时我做错了什么。

I am new to Perl and having some problems figuring out the best way to return multiple arrays from a sub. Here is my code for starters.

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use List::Util;
use Fcntl 'O_RDONLY';
use Tie::File;
use YAML qw();

my $digitData   = 'digitData.txt';
my $alphaData = 'alphaData.txt';

my (@dataA, @dataN) = dataMod();
print Dumper(@dataA);
print Dumper(@dataN);

sub dataMod {
    my (@alphaData, @numData);
    my $fileCount = `wc -l < $alphaData`;;
    chomp $fileCount;
    my $history   = eval {YAML::LoadFile('history.yaml')} || {};

    $history->{$_} && --$history->{$_} for keys %$history;

    tie my @alphas, 'Tie::File', $alphaData, mode => O_RDONLY;
    tie my @nums, 'Tie::File', $digitData, mode => O_RDONLY;

    LINES: for (1 .. $fileCount) {
            my @alphaPool = @alphas;
            my $pair;

            while (@alphaPool) {
                    my @numberPool = @nums;
                    my $tryAlpha = splice @alphaPool, rand(@alphaPool), 1;

                    while (@numberPool) {
                            my $tryNum = splice @numberPool, rand(@numberPool), 1;

                            next if $history->{"$tryAlpha|$tryNum"};

                            @alphas = grep {$_ ne $tryAlpha} @alphas;
                            @numberPool = grep {$_ != $tryNum} @numberPool;
                            #print "$tryAlpha $tryNum\n";
                            push @alphaData, $tryAlpha;
                            push @numData, $tryNum;
                            $history->{"$tryAlpha|$tryNum"} = 5;
                            next LINES;
                    }

                    @alphas = grep {$_ ne $tryAlpha} @alphas;
            }
    }
    YAML::DumpFile('history.yaml', $history);
    return(@alphaData, @numData);
}

I am having trouble figuring out the best way to return the data from the subroutine. I need to conserve or be able to print the data from these two variables: $tryAlpha $tryNum together once they are returned.

Currently it returns each value disjoined. An it appears that only one array has data?

Current output is in this format:

$VAR1 = cellCpe2
$VAR2 = stemClearSte
$VAR3 = OctuStemPr2
$VAR4 = 10
$VAR5 = 30
$VAR6 = 20

The problem is, I would like to use it in the format that is returned when inside the subroutine when the following print statement is executed within the sub:
print "$tryAlpha $tryNum\n";

The result of this print statement, I need to be able to use the same logic for the data as in the print statement:
ie: $varForAlphaData $varForNumData

cellCpe2 10
stemClearSte 30
OctuStemPr2 20

For testing purposes I am using two files
*digitData.txt: Contains three words cellCpe2, stemClearSte, OctuStemPr2. One per line

*alphaData.txt: Contains 10, 20, 30, 40, 50, 60. One per line

Not sure what I am doing wrong at this point.

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

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

发布评论

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

评论(3

ゞ记忆︶ㄣ 2024-12-22 13:55:57

返回参考文献。在列表上下文中使用时,您的两个数组会变平。

return(\@alphaData, \@numData);

用法:

my ($alpha, $num) = dataMod();
for my $item (@$alpha) {
    ...
}

Return references. Your two arrays flatten when used in list context.

return(\@alphaData, \@numData);

Usage:

my ($alpha, $num) = dataMod();
for my $item (@$alpha) {
    ...
}
信仰 2024-12-22 13:55:57

使用数组引用

return(\@alphaData, \@numData);

如何从子例程返回多个数组?

Use array references

return(\@alphaData, \@numData);

How to return multiple arrays from subroutine?

假面具 2024-12-22 13:55:57

由于您想要类似的输出:

cellCpe2 10
stemClearSte 30
OctuStemPr2 20

我假设这些值具有某种关系。如 cellCpe2 的值为 10,stemClearSte 的值为 30,OctuStemPr2 的值为 20。在这种情况下,您可能应该使用哈希而不是两个数组,因为这将以编程方式强制执行关系。这将确保您将相关数据保存在一起。例子...

#declare....
my $data = someFunction();

#print the data
foreach my $key (keys %$data) {

  #The reason the notation is not $data{$key}
  #is because you're NOT using a hash here, but
  # a reference to the hash from someFunction
  my $val = $data->{$key};

  print "\n$key $val";
}

#inside function
sub someFunction() {

  # declare a new hash...
  my %data = ();

  while (aCondition) {
    #do some stuff to define val
    my $varA = $val;
    while (bCondition) {
      #do some stuff to define anotherVal
      my $varB = $anotherVal;

      #save in your hash
      $data{$varA} = $varB;
    }
  }

  #return a reference to your hash
  return \%data;
}

Since you want output like:

cellCpe2 10
stemClearSte 30
OctuStemPr2 20

I'm assuming that those values have some kind of relationship. As in cellCpe2 has a value of 10, stemClearSte has a value of 30, and OctuStemPr2 has a value of 20. In this case, you should probably be using a hash as opposed to two arrays since this will enforce the relationships in a programmatic way. This will ensure that you keep your related data together. Example...

#declare....
my $data = someFunction();

#print the data
foreach my $key (keys %$data) {

  #The reason the notation is not $data{$key}
  #is because you're NOT using a hash here, but
  # a reference to the hash from someFunction
  my $val = $data->{$key};

  print "\n$key $val";
}

#inside function
sub someFunction() {

  # declare a new hash...
  my %data = ();

  while (aCondition) {
    #do some stuff to define val
    my $varA = $val;
    while (bCondition) {
      #do some stuff to define anotherVal
      my $varB = $anotherVal;

      #save in your hash
      $data{$varA} = $varB;
    }
  }

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