Solved

How to pass two arrays to another subroutine in Perl

Posted on 2012-12-20
17
462 Views
Last Modified: 2012-12-20
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
Comment
Question by:Tolgar
  • 9
  • 5
  • 2
  • +1
17 Comments
 
LVL 6

Expert Comment

by:traoher
ID: 38711805
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
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
ID: 38711812
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
 

Author Comment

by:Tolgar
ID: 38711825
@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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38711841
Do you have any code above sub markFailedLines?
0
 
LVL 84

Expert Comment

by:ozo
ID: 38711842
my ($line_numbers, @data) = @_;
0
 
LVL 84

Expert Comment

by:ozo
ID: 38711845
foreach my $ln ($line_numbers) {
There's no reason for such a loop over a single element
0
 

Author Comment

by:Tolgar
ID: 38711850
@Ozo: $line_numbers is a reference to an array. It has more than one value in it.
0
 
LVL 84

Expert Comment

by:ozo
ID: 38711855
markFailedLines(\@line_numbers,\@data);
sub markFailedLines{
my @line_numbers = @{+shift};
my @data = @{+shift};
foreach my $ln (@line_numbers) {
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

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

Author Comment

by:Tolgar
ID: 38711858
@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
 
LVL 84

Expert Comment

by:ozo
ID: 38711859
$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
 

Author Comment

by:Tolgar
ID: 38711861
@Ozo: I am confused. Can you please send me the modified version of my code above?
0
 
LVL 84

Accepted Solution

by:
ozo earned 250 total points
ID: 38711862
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
 
LVL 84

Expert Comment

by:ozo
ID: 38711864
If $line_numbers is always reference,
if ($line_numbers) {
will always be true
0
 
LVL 84

Expert Comment

by:ozo
ID: 38711869
#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
 
LVL 84

Expert Comment

by:ozo
ID: 38711877
#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
 
LVL 84

Expert Comment

by:ozo
ID: 38711882
#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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now