Avatar of Tolgar
Tolgar
 asked on

How to pass two arrays to another subroutine in Perl

How can I pass two arrays to another subroutine?

Example:

my ($result, @failedLineBits) = &helperFunctions::markFailedLines(@line_numbers,@data);

Open in new window


package helperFunctions;		
		
use strict;
use warnings;

sub markFailedLines{
my (@line_numbers, @data) = @_;

SOME CODE IN HERE

return ($result,@out);
}
1;

Open in new window

Perl

Avatar of undefined
Last Comment
ozo

8/22/2022 - Mon
traoher

combine the two arrays into a single array using "push"

example:

@time = ("hour","minutes");
@date =("day","year");
Push (@time, @date);

Once you have the single array, pass the single array to the sub.
SOLUTION
kaufmed

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Tolgar

ASKER
@kaufmed: Ok. I tried your solution and I am getting "Global symbol "@data" requires explicit package name" error.

Here is my whole function:

sub markFailedLines{
my ($line_numbers, $data) = @_;
my @out;
	if ($line_numbers) {
		foreach my $ln ($line_numbers) {
			$data[$ln+2] = "<font color=\"red\">$data[$ln+2]</font>";
			push(@out, '<br><br>');
			push @out, join('<br>', map{ join(': ', $_, $data[$_+2]) } $ln-3..$ln+3);
		}
		return ('FAILED',@out);
	} else {
		return ('PASSED',@out);
	}
}

Open in new window


What am I doing wrong here?
kaufmed

Do you have any code above sub markFailedLines?
Your help has saved me hundreds of hours of internet surfing.
fblack61
ozo

my ($line_numbers, @data) = @_;
ozo

foreach my $ln ($line_numbers) {
There's no reason for such a loop over a single element
Tolgar

ASKER
@Ozo: $line_numbers is a reference to an array. It has more than one value in it.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ozo

markFailedLines(\@line_numbers,\@data);
sub markFailedLines{
my @line_numbers = @{+shift};
my @data = @{+shift};
foreach my $ln (@line_numbers) {
Tolgar

ASKER
@kuafmed: Yes there is another sub function. But the error is for the lines 6 and 8 in the above code.
Tolgar

ASKER
@Ozo: Why do I need to use shift? I don't find it very secure to use.
 Is there a way of modifying my code above without using shift?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ozo

$line_numbers is a reference to an array. It has more than one value in it.
It is a single reference.
 foreach my $ln ($line_numbers) {
would alias $ln to the single reference
 foreach my $ln (@$line_numbers) {
would alias $ln to each of the elements in the referenced array
Tolgar

ASKER
@Ozo: I am confused. Can you please send me the modified version of my code above?
ASKER CERTIFIED SOLUTION
ozo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ozo

If $line_numbers is always reference,
if ($line_numbers) {
will always be true
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ozo

#If you call
markFailedLines(\@line_numbers,\@data);
#then you can use
sub markFailedLines{
my ($line_numbers, $data) = @_;
my @out;
            foreach my $ln (@$line_numbers) {
                  $data->[$ln+2] = "<font color=\"red\">$data->[$ln+2]</font>";   #note that this modifies the original @data
                  push @out, '<br><br>',join('<br>', map{ join(': ', $_, $data->[$_+2]) } $ln-3..$ln+3);
            }
            return @out?'PASSED':'FAILED',@out;  #this will pass when @line_numbers is not empty
}
ozo

#If you want to pass a copy of @data to prevent the sub from modifying the original, you can call
markFailedLines(\@line_numbers,[@data]);
ozo

#Or you can pass only the first array as a reference:
markFailedLines(\@line_numbers,@data);

sub markFailedLines{
my ($line_numbers, @data) = @_;
my @out;
            foreach my $ln (@$line_numbers) {
                  $data[$ln+2] = "<font color=\"red\">$data[$ln+2]</font>";  
                  push @out, '<br><br>',join('<br>', map{ join(': ', $_, $data[$_+2]) } $ln-3..$ln+3);
            }
            return @out?'PASSED':'FAILED',@out;
}
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23