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

  • Home
  • SEARCH
  • 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 9274709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:24:58+00:00 2026-06-18T16:24:58+00:00

As the title says, perl adds dummy elements to arrays after inquiries to not

  • 0

As the title says, perl adds dummy elements to arrays after inquiries to not existing elements. Array size grows after the inquiry. Illustration to the behaviour:

    my $rarr;
    $rarr->[0][0] = 'S';
    $rarr->[0][1] = 'MD';
    $rarr->[1][0] = 'S';
    $rarr->[1][1] = 'PRP';

    my $crulesref;
    $crulesref->[0]  = $rarr;

     check_rule('aa', 0);
     if($rarr->[3][0] == 'M'){  # just check a not existing element
        print "m\n";   
     }

     check_rule('bb', 0);
     if($rarr->[5][0] == 'M'){  # again: just check a not existing element
        print "m\n";
     }
     check_rule('cc', 0);


     sub check_rule($$)
     {
         my ($strg,$ix) = @_;
         my $aref = $crulesref->[$ix];
         my $rule_size = @$aref;
         {print "-----$strg aref:$aref rs:$rule_size aref:'@$aref'\n";
           for(my $t1 = 0; $t1 <$rule_size; $t1++){
             print "t1:$t1 0:$aref->[$t1][0] 1:$aref->[$t1][1]\n";
           }
         }
       }

The result of the run is:

    en@en-desktop ~/dtest/perl/forditas/utf8_v1/forditas/test1 $ perl v15.pl
    -----aa aref:ARRAY(0x90ed8c8) rs:2 aref:'ARRAY(0x9106cac) ARRAY(0x9106d24)'
    t1:0 0:S 1:MD
    t1:1 0:S 1:PRP
    m                     <-------------- finds the non existing
    -----bb aref:ARRAY(0x90ed8c8) rs:4 aref:'ARRAY(0x9106cac) ARRAY(0x9106d24)          ARRAY(0x9107508)'
    t1:0 0:S 1:MD
    t1:1 0:S 1:PRP
    t1:2 0: 1:               <-- undesired dummy due to inquiry
    t1:3 0: 1:               <-- undesired dummy due to inquiry
    m                      <-------------- finds the non existing
    -----cc aref:ARRAY(0x90ed8c8) rs:6 aref:'ARRAY(0x9106cac) ARRAY(0x9106d24) ARRAY(0x9107904) ARRAY(0x9107508)  ARRAY(0x910e860)'
    t1:0 0:S 1:MD
    t1:1 0:S 1:PRP
    t1:2 0: 1:               <-- undesired dummy due to inquiry
    t1:3 0: 1:               <-- undesired dummy due to inquiry
    t1:4 0: 1:               <-- undesired dummy due to inquiry
    t1:5 0: 1:               <-- undesired dummy due to inquiry

Is there no other way to avoid this than to ask before each inquiry, if the inquired element exists? I try to increase speed, and these inquiries slow the code down, and make it less easy to read.

Thanks in advance for useful hints.

  • 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-18T16:24:59+00:00Added an answer on June 18, 2026 at 4:24 pm

    This is autovivification that you are seeing. If you access the memory of $ref->[3][0] even with just a check:

    if ($ref->[3][0] eq 'M' )
    

    Then first $ref->[3] must exist before its element number zero can be checked, so it is created via autovivification. You need to first check if $ref->[3] exists or is defined to avoid creating it.

    if (defined($ref->[3]) && $ref->[3][0] eq 'M')
    

    Also, you should always use:

    use strict;
    use warnings;
    

    Then you would see the warnings

    Argument "M" isn't numeric in numeric eq (==) at ...
    Use of uninitialized value in numeric eq (==) at ...
    

    The if-clause gives a false positive here because the string 'M' is converted to a number (0) because of the context imposed by the numeric equality operator ==. The LHS value is undef, which is also converted to a number (0), which is why the expression evaluates to true.

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

Sidebar

Related Questions

As title says for some purpose I need to get the size of request/response
The title says it all, but I will provide more clarification: After seeing many
Title says it all. Note that this is not about a change in the
The title sort of says it all. I have a text file (NOT a
As the title says, in Perl, how can I save a hash that contains
Title says all. Sample code: ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>(); HashMap<String, Object>
As title says, I have a list of words, Like stopWords = [the, and,
As title says, i know that new throws an exception which can be caught,
As title says. I want to perform and order by on a list -
As title says, I have access to the current listitem - and it's easy

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.