Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8801211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:46:39+00:00 2026-06-14T00:46:39+00:00

My question in Perl is: Define 2×2 arrays using anonymous lists. Pass the arrays

  • 0

My question in Perl is:
Define 2×2 arrays using anonymous lists. Pass the arrays to a subroutine and add them together. Return a reference to sum array and print the values from the main part of the program.

My script is:

#!/usr/bin/perl

use strict;

use warnings;

my @array = ([1,2],[4,5]); 

my $refarray = \@array;

print sumarray($refarray);

sub sumarray

{

 $refarray = shift;

 foreach (@{$refarray})

 {

  $refarray = ($refarray[0]->[0]+$refarray[1]->[0],$refarray[0]->[1]+$refarray[1]->[1]);

 }

 return $refarray;

}

Where am I going wrong? Please help. Thanks in advance.

I am getting the output as 0.

If I use use strict; and use warnings; I will get the error message as

Global symbol "@refarray" requires explicit package name at line 23.
Global symbol "@refarray" requires explicit package name at line 23.
Global symbol "@refarray" requires explicit package name at line 23.
Global symbol "@refarray" requires explicit package name at line 23.
Execution aborted due to compilation errors.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:46:40+00:00Added an answer on June 14, 2026 at 12:46 am

    Few problems with your code: –

    • First, in your for-loop, you are modifying your reference $refarray which you should not do.
    • Second, $refarray[0]->[0] will not compile. Since $refarray is a reference to an array, you should either use its 1st element using arrow: – $refarray->[0][0], or you need to de-reference it before using the way you are using: – $$refarray[0]->[0].

    Having said that, I think you should replace your subroutine with this one: –

    use strict;
    use warnings;
    
    my @array = ([1,2],[4,5]); 
    my $refarray = \@array;
    
    my $sum = sumarray($refarray);
    print $sum->[0], $sum->[1];
    
    sub sumarray {
        my $ref = shift;
        return [$ref->[0][0] + $ref->[1][0], $ref->[0][1] + $ref->[1][1]];
    }
    

    OUTPUT: –

    5 7
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My question in Perl: Write a Perl Script with a subroutine that accepts a
This is a beginner-best-practice question in perl. I'm new to this language. The question
I have written a Perl script for the following bioinformatics question, but unfortunately there
maybe this is a stupid question but : i run perl 5.8.8 and i
I am very new to Perl, so please bear with my simple question: Here
Recently, I was asked in an interview question to reverse a string in perl.
Okay, so I'm using perl to read in a file that contains some general
I am writing a script in Perl and have a question about Perl's foreach
My question in Perl is: Perl Script which will read the text (no punctuation
The question is: Perl script that counts the number of times each digit appears

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.