• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 494
  • Last Modified:

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

0
Tolgar
Asked:
Tolgar
  • 9
  • 5
  • 2
  • +1
2 Solutions
 
traoherCommented:
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.
0
 
käµfm³d 👽Commented:
Pass the arrays as references:

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

Open in new window


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

    print @{$line_numbers};
    print @{$data};

    return ($result,@out);
}

Open in new window


http://www.cs.cf.ac.uk/Dave/PERL/node61.html
0
 
TolgarAuthor Commented:
@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?
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
käµfm³d 👽Commented:
Do you have any code above sub markFailedLines?
0
 
ozoCommented:
my ($line_numbers, @data) = @_;
0
 
ozoCommented:
foreach my $ln ($line_numbers) {
There's no reason for such a loop over a single element
0
 
TolgarAuthor Commented:
@Ozo: $line_numbers is a reference to an array. It has more than one value in it.
0
 
ozoCommented:
markFailedLines(\@line_numbers,\@data);
sub markFailedLines{
my @line_numbers = @{+shift};
my @data = @{+shift};
foreach my $ln (@line_numbers) {
0
 
TolgarAuthor Commented:
@kuafmed: Yes there is another sub function. But the error is for the lines 6 and 8 in the above code.
0
 
TolgarAuthor Commented:
@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?
0
 
ozoCommented:
$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
0
 
TolgarAuthor Commented:
@Ozo: I am confused. Can you please send me the modified version of my code above?
0
 
ozoCommented:
Using references
                        $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);
0
 
ozoCommented:
If $line_numbers is always reference,
if ($line_numbers) {
will always be true
0
 
ozoCommented:
#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
}
0
 
ozoCommented:
#If you want to pass a copy of @data to prevent the sub from modifying the original, you can call
markFailedLines(\@line_numbers,[@data]);
0
 
ozoCommented:
#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;
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 9
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now