Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl returing multiData from sub

Tags:

perl

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.

like image 656
user1048209 Avatar asked Jun 16 '26 14:06

user1048209


1 Answers

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

return(\@alphaData, \@numData);

Usage:

my ($alpha, $num) = dataMod();
for my $item (@$alpha) {
    ...
}
like image 89
Oesor Avatar answered Jun 18 '26 03:06

Oesor