Perl 从 sub 返回 multiData
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
返回参考文献。在列表上下文中使用时,您的两个数组会变平。
用法:
Return references. Your two arrays flatten when used in list context.
Usage:
使用数组引用
如何从子例程返回多个数组?
Use array references
How to return multiple arrays from subroutine?
由于您想要类似的输出:
我假设这些值具有某种关系。如 cellCpe2 的值为 10,stemClearSte 的值为 30,OctuStemPr2 的值为 20。在这种情况下,您可能应该使用哈希而不是两个数组,因为这将以编程方式强制执行关系。这将确保您将相关数据保存在一起。例子...
Since you want output like:
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...