Perl从sub返回多个数据

时间:2011-11-15 19:23:32

标签: perl

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

#!/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

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

这个print语句的结果,我需要能够使用与print语句中相同的逻辑数据: 即:$ varForAlphaData $ varForNumData

cellCpe2 10
stemClearSte 30
OctuStemPr2 20

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

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

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

3 个答案:

答案 0 :(得分:5)

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

return(\@alphaData, \@numData);

用法:

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

答案 1 :(得分:5)

使用数组引用

return(\@alphaData, \@numData);

How to return multiple arrays from subroutine?

答案 2 :(得分:3)

因为您需要输出:

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;
}